Book Image

SQL Server 2014 with PowerShell v5 Cookbook

By : Donabel Santos
Book Image

SQL Server 2014 with PowerShell v5 Cookbook

By: Donabel Santos

Overview of this book

Table of Contents (21 chapters)
SQL Server 2014 with PowerShell v5 Cookbook
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Listing logins, users, and database mappings


This recipe lists logins and their corresponding usernames through database mappings.

Getting ready

To check logins and their database mappings in SQL Server Management Studio, log in to SSMS. Go to the Security folder, expand Logins, and double-click on a particular login. This will show you the Login Properties window. Click on the User Mapping option on the left-hand pane, as shown in the following screenshot:

How to do it...

To list logins, users, and database mappings, let's use the following steps:

  1. Open PowerShell ISE as an administrator.

  2. Import the SQLPS module and create a new SMO Server Object as follows:

    #import SQL Server module
    Import-Module SQLPS -DisableNameChecking
    
    #replace this with your instance name
    $instanceName = "localhost"
    $server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $instanceName

    Add the following script and run:

    #display login info
    #these are two different ways of displaying login info...