|
| 1 | +[CmdletBinding()] |
| 2 | +param ( |
| 3 | + # The name of the SQL Instance running on the node. |
| 4 | + [String[]]$instance_name, |
| 5 | + # The name of the SQL Login to get information about. |
| 6 | + [string[]]$login_name, |
| 7 | + # If true this will force the name to match a login exactly to be returned. |
| 8 | + [switch]$exact_match, |
| 9 | + # Return more detailed information about logins including SID's. |
| 10 | + [switch]$detailed |
| 11 | +) |
| 12 | + |
| 13 | +$error = @{ |
| 14 | + _error = @{ |
| 15 | + msg = '' |
| 16 | + kind = 'puppetlabs.task/task-error' |
| 17 | + details = @{ |
| 18 | + detailedInfo = '' |
| 19 | + exitcode = 1 |
| 20 | + } |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +function Select-LoginName { |
| 25 | + param( |
| 26 | + [PSObject]$login, |
| 27 | + [string[]]$namesToMatch, |
| 28 | + [switch]$exact_match |
| 29 | + ) |
| 30 | + |
| 31 | + |
| 32 | + # This function takes a single SQLServer login object and compares it against |
| 33 | + # the list of names passed into the -login_name parameter of the script to |
| 34 | + # determine if this is a login the user is interested in seeing. If it does |
| 35 | + # not pass the filter represented by that parameter the login is discarded. |
| 36 | + |
| 37 | + foreach ($paramLogin in $namesToMatch) { |
| 38 | + if ($exact_match) { |
| 39 | + if ($paramLogin -eq $login.name) { |
| 40 | + Write-Output $login |
| 41 | + } |
| 42 | + } |
| 43 | + else { |
| 44 | + # Match is a regex operator, and it doesn't like the '\' in domain names. |
| 45 | + if ($login.name -match [regex]::escape($paramLogin)) { |
| 46 | + Write-Output $login |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +function Get-SQLInstances { |
| 53 | + param( |
| 54 | + [string[]]$instance_name |
| 55 | + ) |
| 56 | + |
| 57 | + $instancesHolder = New-Object System.Collections.Generic.List[System.Object] |
| 58 | + $stringsToReturn = New-Object System.Collections.Generic.List[System.Object] |
| 59 | + |
| 60 | + # The default instance is referred to in its service name as MSSQLSERVER. This |
| 61 | + # leads many SQLSERVER people to refer to it as such. They will also connect |
| 62 | + # to it using just a '.'. None of these are its real name. Its real instance |
| 63 | + # name is just the machine name. A named instances real name is the machine |
| 64 | + # name a, '\', and the instance name. This little foreach ensures that we are |
| 65 | + # referring to these instances by their real names so that proper filtering |
| 66 | + # can be done. |
| 67 | + |
| 68 | + foreach ($name in $instance_name) { |
| 69 | + switch ($name) { |
| 70 | + {($_ -eq 'MSSQLSERVER') -or ($_ -eq '.')} { [void]$instancesHolder.add($env:COMPUTERNAME) } |
| 71 | + {$_ -eq $env:COMPUTERNAME} { [void]$instancesHolder.add($_) } |
| 72 | + {$_ -notmatch '\\'} { [void]$instancesHolder.add("$env:COMPUTERNAME\$_") } |
| 73 | + default { [void]$instancesHolder.add($name) } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + $instanceStrings = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server').InstalledInstances |
| 78 | + |
| 79 | + # The registry key does not return the real instance names. Again we must |
| 80 | + # normalize these names into their real names so that comparisons can be done |
| 81 | + # properly. |
| 82 | + |
| 83 | + foreach ($string in $instanceStrings) { |
| 84 | + switch ($string) { |
| 85 | + 'MSSQLSERVER' { $string = $env:COMPUTERNAME } |
| 86 | + Default {$string = "$env:COMPUTERNAME\$string"} |
| 87 | + } |
| 88 | + |
| 89 | + if ((-not [string]::IsNullOrEmpty($instancesHolder))-and(-not [string]::IsNullOrWhiteSpace($instancesHolder))) { |
| 90 | + foreach ($instance in $instancesHolder) { |
| 91 | + if ($instance -eq $string) { |
| 92 | + [void]$stringsToReturn.add($string) |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + else { |
| 97 | + [void]$stringsToReturn.add($string) |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + if($stringsToReturn.count -gt 0){ |
| 102 | + Write-Output $stringsToReturn |
| 103 | + } else { |
| 104 | + throw "No instances were found by the name(s) $instance_name" |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +function Get-ServerObject { |
| 109 | + param( |
| 110 | + [string]$instance |
| 111 | + ) |
| 112 | + |
| 113 | + [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") |
| 114 | + |
| 115 | + Write-Output (New-Object Microsoft.SqlServer.Management.Smo.Server -ArgumentList $instance) |
| 116 | + |
| 117 | +} |
| 118 | + |
| 119 | +$return = @{} |
| 120 | + |
| 121 | +#Get SQL Instances |
| 122 | + |
| 123 | +try { |
| 124 | + $SQLInstances = Get-SQLInstances -instance_name $instance_name |
| 125 | +} |
| 126 | +catch { |
| 127 | + $error._error.msg = 'Cannot detect SQL instance names.' |
| 128 | + $error._error.details.detailedInfo = $_ |
| 129 | + return $error | ConvertTo-JSON |
| 130 | +} |
| 131 | + |
| 132 | +# Unfiltered Logins from all instances. |
| 133 | +$rawLogins = New-Object System.Collections.Generic.List[System.Object] |
| 134 | + |
| 135 | +foreach ($instance in $SQLInstances) { |
| 136 | + try { |
| 137 | + $sqlServer = Get-ServerObject -instance $instance |
| 138 | + } |
| 139 | + catch { |
| 140 | + $error._error.msg = "Cannot connect to SQL Instance: $instance" |
| 141 | + $error._error.details.detailedInfo = $_ |
| 142 | + return $error | ConvertTo-JSON |
| 143 | + } |
| 144 | + |
| 145 | + foreach ($item in $sqlServer.logins) { |
| 146 | + # The login object doesn't return information about which instance it |
| 147 | + # came from. This could be a problem on a machine running more than |
| 148 | + # one instance. We'll add a property here to make sure we don't lose |
| 149 | + # track of this information. |
| 150 | + |
| 151 | + Add-Member -InputObject $item -MemberType NoteProperty -Name 'InstanceName' -Value $instance |
| 152 | + |
| 153 | + [void]$rawLogins.add($item) |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +# Filtered logins based on the filter passed into the $login_name parameter |
| 158 | +$logins = New-Object System.Collections.Generic.List[System.Object] |
| 159 | + |
| 160 | +foreach ($login in $rawLogins) { |
| 161 | + if ($MyInvocation.BoundParameters.ContainsKey('login_name')) { |
| 162 | + [void]$logins.add((Select-LoginName -login $login -namesToMatch $login_name -exact_match:$exact_match)) |
| 163 | + } |
| 164 | + else { |
| 165 | + [void]$logins.add($login) |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +if ($detailed) { |
| 170 | + |
| 171 | + # The SID property of the Login object contains an array of integers. This |
| 172 | + # is not how SQL Server represents the login int he sys.server_principals |
| 173 | + # table. The array of integers needs to be converted one by one into their |
| 174 | + # Hex representation, and then joined together. This will match the value |
| 175 | + # in the server_principals table and allow someone executing this script |
| 176 | + # to correlate users properly. |
| 177 | + |
| 178 | + $sidFunction = { |
| 179 | + $finalSid = '0x' |
| 180 | + foreach ($segment in $_.sid) { |
| 181 | + $finalSid += ("{0:x2}" -f $segment).ToUpper() |
| 182 | + } |
| 183 | + $finalSid |
| 184 | + } |
| 185 | + |
| 186 | + # The SID column of sys.server_principals stores the binary representation |
| 187 | + # of an account SID. This is not a SID that can easily be correlated to an |
| 188 | + # Active Directory style SID that you can find in most other tools. This |
| 189 | + # function converts SQLServer's binary sid into a SID string that people |
| 190 | + # are more familiar with. |
| 191 | + |
| 192 | + $winSidFunction = { |
| 193 | + (New-Object System.Security.Principal.SecurityIdentifier($_.sid, 0)).toString() |
| 194 | + } |
| 195 | + |
| 196 | + $properties = @( |
| 197 | + 'Name' |
| 198 | + @{N = 'CreateDate'; E = {"$($_.CreateDate)"}} |
| 199 | + @{N = 'DateLastModified'; E = {"$($_.DateLastModified)"}} |
| 200 | + 'InstanceName' |
| 201 | + 'DefaultDatabase' |
| 202 | + 'DenyWindowsLogin' |
| 203 | + 'HasAccess' |
| 204 | + 'ID' |
| 205 | + 'IsDisabled' |
| 206 | + 'IsLocked' |
| 207 | + 'IsPasswordExpired' |
| 208 | + 'IsSystemObject' |
| 209 | + 'Language' |
| 210 | + 'LanguageAlias' |
| 211 | + 'LoginType' |
| 212 | + 'MustChangePassword' |
| 213 | + 'PasswordExpirationEnabled' |
| 214 | + 'PasswordHashAlgorithm' |
| 215 | + 'PasswordPolicyEnforced' |
| 216 | + @{N = 'SQLSID'; E = $sidFunction} |
| 217 | + @{N = 'ADSid'; E = $winSidFunction} |
| 218 | + 'WindowsLoginAccessType' |
| 219 | + 'UserData' |
| 220 | + 'State' |
| 221 | + 'IsDesignMode' |
| 222 | + ) |
| 223 | + |
| 224 | + $return.logins = $logins | Select-Object $properties |
| 225 | + $return | ConvertTo-JSON -Depth 99 |
| 226 | +} |
| 227 | +else { |
| 228 | + $properties = @( |
| 229 | + 'Name' |
| 230 | + 'isDisabled' |
| 231 | + 'isLocked' |
| 232 | + 'IsPasswordExpired' |
| 233 | + @{N = 'CreateDate'; E = {"$($_.CreateDate)"}} |
| 234 | + @{N = 'DateLastModified'; E = {"$($_.DateLastModified)"}} |
| 235 | + ) |
| 236 | + |
| 237 | + $return.logins = $logins | Select-Object $properties |
| 238 | + $return | ConvertTo-JSON -Depth 99 |
| 239 | +} |
| 240 | + |
| 241 | +<# |
| 242 | +.SYNOPSIS |
| 243 | + This script connects to a SQL instance running on a machine and returns |
| 244 | + information about logins. |
| 245 | +.DESCRIPTION |
| 246 | + This script will connect to SQL instances running on a machine and return |
| 247 | + information about logins configured on the instance. This script only connects |
| 248 | + to instances on a local server. It will always return data in JSON format. |
| 249 | +.PARAMETER instance_name |
| 250 | + The name of the instance running on a machine that you would like to connect to. |
| 251 | + Leave blank to get the default instance MSSQLSERVER. |
| 252 | +#> |
0 commit comments