Powershell and SQL Server – 5- List Login Information from Multiple SQL Server Instance with Invoke-Sqlcmd

The script pull Login information from single or multiple SQL Instance

cls

if (-not (Get-Module SQLPS)) 
    {Import-Module SQLPS} 

#Get list of server from text files

$SQLserverDB=@() 
$SQLserverDB=Get-Content "C:\PowershellScript\ServerList.txt"

    foreach($SqlServer in $SQLserverDB)
    {
    Write-Host "|- SQL Server : " $SqlServer -ForegroundColor Yellow

        
        try
        {
        
            $result=Invoke-Sqlcmd -ServerInstance $SqlServer  -Database master -Query "SELECT * FROM sys.syslogins" -QueryTimeout 0 -ErrorAction Stop
 
            $result |SELECT name,createdate,updatedate,hasaccess,isntname,isntgroup,isntuser,sysadmin,securityadmin,serveradmin,setupadmin,processadmin,diskadmin,dbcreator,bulkadmin,loginname|ft -AutoSize
        }
        catch
        {
            $ErrorMessage = $_.Exception.Message
            $FailedItem = $_.Exception.ItemName

            Write-Host $_.Exception.Message "-" $_.Exception.ItemName
        }
     }

PSLogins

 

Leave a Reply

Your email address will not be published. Required fields are marked *