1

I want to write a report that gets all the permissions for every Login in our SQL server farm. For now I can get the logins but get stuck there trying to connect the logins to roles. Haven't even found a way to get the roles yet. Are there PowerShell functions available for this or do I need to write sql statements and parse the results with PowerShell? Alternatively, Am i making it too complicated and should get to the information from a different angle?

#Get all the logins for every server
#Filters out all "NT Service" and "NT Authority" accounts as well as MSPolicy Accounts
$SQLLogins = Get-SqlLogin -ServerInstance $DefaultServerList | Where {($_.name -notmatch "^NT Service\\.*") -and ($_.name -notmatch "^NT AUTHORITY\\.*") -and ($_.name -notmatch "##.*##")}

#For every one, determine the permissions
ForEach ($Login in $SQLLogins) {
    Write-Host $Login.name "    " $Login.Parent.Name
}

3 Answers 3

3

As @user1716729 noted in their answer, dbatools can do this quite well. In fact, I have a script that produces exactly such a report. I can't share the whole thing as I wrote it expressly for my employer, but I do the following:

This takes less than 10 seconds to execute against my estate of about 16 instances and my security folks love the results.

1
  • Is there nothing from Microsoft to transfer grants to another server?
    – variable
    Commented Apr 7, 2022 at 6:51
2

I am not 100% sure but dbatools.io might have one.

2
  • I ended up using Get-DbaDbRoleMember Commented May 4, 2020 at 12:57
  • Is there nothing from Microsoft to transfer grants to another server?
    – variable
    Commented Apr 7, 2022 at 6:51
1

Are there PowerShell functions available for this or do I need to write sql statements and parse the results with PowerShell?

In general you switch to using SQL through Invoke-Sqlcmd whenever it's not obvious how to do something with the powershell cmdlets. Here you would just query sys.server_role_members for the login's server roles.

3
  • This is my fallback solution. However, I was looking for something that can get me the results without having to resort to SQL in the hopes to get the results faster. Commented May 4, 2020 at 12:48
  • Yep. SQLPS is just not that full-featured, and it's built on top of SMO, which is full-featured, but is pretty old and crufty. TSQL is the main administrative interface for SQL Server. That's what most people use, and that's what you'll find solutions in when searching on the web, and what's the most fully documented. Commented May 4, 2020 at 13:19
  • Is there nothing from Microsoft to transfer grants to another server?
    – variable
    Commented Apr 7, 2022 at 6:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.