functions/Test-DbaSpn.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
Function Test-DbaSpn
{
<#
.SYNOPSIS
Test-DbaSpn will determine what SPNs *should* be set for a given server (and any instances of SQL running on it) and return
whether the SPNs are set or not.
 
.DESCRIPTION
This function is designed to take in a server name(s) and attempt to determine required SPNs. It was initially written to mimic the (previously)
broken functionality of the Microsoft Kerberos Configuration manager and SQL Server 2016. The functon will connect to a remote server and,
through WMI, discover all running intances of SQL Server. For any instances with TCP/IP enabled, the script will determine which port(s)
the instances are listening on and generate the required SPNs. For named instances NOT using dynamic ports, the script will generate a port-
based SPN for those instances as well. At a minimum, the script will test a base, port-less SPN for each instance discovered.
 
Once the required SPNs are generated, the script will connect to Active Directory and search for any of the SPNs (if any) that are already
set.
 
The function will return a custom object(s) that contains the server name checked, the instance name discovered, the account the service is
running under, and what the "required" SPN should be. It will also return a boolean property indicating if the SPN is set in Active Directory
or not.
 
.PARAMETER ComputerName
The server name you want to discover any SQL Server instances on. This parameter is required.
 
.PARAMETER Credential
The credential you want to use to connect to the remote server and active directory. This parameter is required.
 
.PARAMETER Domain
If your server resides on a different domain than what your current session is authenticated against, you can specify a domain here. This
parameter is optional.
 
.NOTES
Tags: SQLWMI, SPN
Author: Drew Furgiuele (@pittfurg), http://www.port1433.com
 
dbatools PowerShell module (https://dbatools.io, clemaire@gmail.com)
 
Copyright (C) 2016 Chrissy LeMaire
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
 
.LINK
https://dbatools.io/Test-DbaSpn
 
.EXAMPLE
Test-DbaSpn -ComputerName SQLSERVERA -Credential (Get-Credential)
 
Connects to a computer (SQLSERVERA) and queries WMI for all SQL instances and return "required" SPNs. It will then take each SPN it generates
and query Active Directory to make sure the SPNs are set.
 
.EXAMPLE
Test-DbaSpn -ComputerName SQLSERVERA,SQLSERVERB -Credential (Get-Credential)
 
Connects to multiple computers (SQLSERVERA, SQLSERVERB) and queries WMI for all SQL instances and return "required" SPNs.
It will then take each SPN it generates and query Active Directory to make sure the SPNs are set.
 
.EXAMPLE
Test-DbaSpn -ComputerName SQLSERVERC -Domain domain.something -Credential (Get-Credential)
 
Connects to a computer (SQLSERVERC) on a specified and queries WMI for all SQL instances and return "required" SPNs.
It will then take each SPN it generates and query Active Directory to make sure the SPNs are set. Note that the credential you pass must
have be a valid login with appropriate rights on the domain you specify
 
#>

[cmdletbinding()]
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[string[]]$ComputerName,
[Parameter(Mandatory = $false)]
[PSCredential]$Credential,
[Parameter(Mandatory = $false)]
[string]$Domain
)

process
{
foreach ($computer in $computername)
{
try
{
$resolved = Resolve-DbaNetworkName -ComputerName $computer -Credential $Credential -ErrorAction Stop
}
catch
{
$resolved = Resolve-DbaNetworkName -ComputerName $computer -Turbo
}

$ipaddr = $resolved.IPAddress
$hostentry = $resolved.DNSHostEntry

if (!$domain)
{
$domain = $resolved.domain
if ($computer -notmatch "\.")
{
$computer = $resolved.DNSHostEntry
}
}
else
{
if ($computer -notmatch "\.")
{
if ($computer -match "\\")
{
$computer = $computer.Split("\")[0]

}
$computer = "$computer.$domain"
}
}

Write-Verbose "Resolved ComputerName to FQDN: $computer"

$Scriptblock = {

Function Convert-SqlVersion
{
param (
[version]$version
)

switch ($version.Major)
{
9 { "SQL Server 2005" }
10 {
if ($version.Minor -eq 0)
{
"SQL Server 2008"
}
else
{
"SQL Server 2008 R2"
}
}
11 { "SQL Server 2012" }
12 { "SQL Server 2014" }
13 { "SQL Server 2016" }
14 { "SQL Server vNext" }
default { $version }
}
}

$spns = @()
$servername = $args[0]
$hostentry = $args[1]
$domain = $args[2]
$instancecount = $wmi.ServerInstances.Count
Write-Verbose "Found $instancecount instances"

foreach ($instance in $wmi.ServerInstances)
{
$spn = [pscustomobject] @{
ComputerName = $servername
InstanceName = $null
SqlProduct = $null #SKUNAME
InstanceServiceAccount = $null
RequiredSPN = $null
IsSet = $false
Cluster = $false
TcpEnabled = $false
Port = $null
DynamicPort = $false
Warning = "None"
Error = "None"
Credential = $Credential # for piping
}

$spn.InstanceName = $instance.name
$InstanceName = $spn.InstanceName

Write-Verbose "Parsing $InstanceName"

$services = $wmi.services | Where-Object DisplayName -eq "SQL Server ($InstanceName)"
$spn.InstanceServiceAccount = $services.ServiceAccount
$spn.Cluster = ($services.advancedproperties | Where-Object Name -eq 'Clustered').Value

if ($spn.Cluster)
{
$hostentry = ($services.advancedproperties | Where-Object Name -eq 'VSNAME').Value.ToLower()
Write-Verbose "Found cluster $hostentry"
$hostentry = ([System.Net.Dns]::GetHostEntry($hostentry)).HostName
$spn.ComputerName = $hostentry
}

$rawversion = [version]($services.advancedproperties | Where-Object Name -eq 'VERSION').Value #13.1.4001.0

$version = Convert-SqlVersion $rawversion
$skuname = ($services.advancedproperties | Where-Object Name -eq 'SKUNAME').Value

$spn.SqlProduct = "$version $skuname"

#is tcp enabled on this instance? If not, we don't need an spn, son
if ((($instance.serverprotocols | Where-Object { $_.Displayname -eq "TCP/IP" }).ProtocolProperties | Where-Object { $_.Name -eq "Enabled" }).Value -eq $true)
{
Write-Verbose "TCP is enabled, gathering SPN requirements"
$spn.TcpEnabled = $true
#Each instance has a default SPN of MSSQLSvc\<fqdn> or MSSSQLSvc\<fqdn>:Instance
if ($instance.Name -eq "MSSQLSERVER")
{
$spn.RequiredSPN = "MSSQLSvc/$hostentry"
}
else
{
$spn.RequiredSPN = "MSSQLSvc/" + $hostentry + ":" + $instance.name
}
}

$spns += $spn
}
# Now, for each spn, do we need a port set? Only if TCP is enabled and NOT DYNAMIC!
ForEach ($spn in $spns)
{
$ports = @()

$ips = (($wmi.ServerInstances | Where-Object { $_.name -eq $spn.InstanceName }).ServerProtocols | Where-Object { $_.DisplayName -eq "TCP/IP" -and $_.IsEnabled -eq "True" }).IpAddresses
$ipAllPort = $null
ForEach ($ip in $ips)
{
if ($ip.Name -eq "IPAll")
{
$ipAllPort = ($ip.IPAddressProperties | Where-Object { $_.Name -eq "TCPPort" }).Value
if (($ip.IpAddressProperties | Where-Object { $_.Name -eq "TcpDynamicPorts" }).Value -ne "")
{
$ipAllPort = ($ip.IPAddressProperties | Where-Object { $_.Name -eq "TcpDynamicPorts" }).Value + "d"
}

}
else
{
$enabled = ($ip.IPAddressProperties | Where-Object { $_.Name -eq "Enabled" }).Value
$active = ($ip.IPAddressProperties | Where-Object { $_.Name -eq "Active" }).Value
$TcpDynamicPorts = ($ip.IPAddressProperties | Where-Object { $_.Name -eq "TcpDynamicPorts" }).Value
if ($enabled -and $active -and $TcpDynamicPorts -eq "")
{
$ports += ($ip.IPAddressProperties | Where-Object { $_.Name -eq "TCPPort" }).Value
}
elseif ($enabled -and $active -and $TcpDynamicPorts -ne "")
{
$ports += $ipAllPort + "d"
}
}
}
if ($ipAllPort -ne "")
{
#IPAll overrides any set ports. Not sure why that's the way it is?
$ports = $ipAllPort
}

$ports = $ports | Select-Object -Unique
ForEach ($port in $ports)
{
$newspn = $spn.PSObject.Copy()
if ($port -like "*d")
{
$newspn.Port = ($port.replace("d", ""))
$newspn.RequiredSPN = $newspn.RequiredSPN.Replace($newSPN.InstanceName, $newspn.Port)
$newspn.DynamicPort = $true
$newspn.Warning = "Dynamic port is enabled"
}
else
{
#If this is a named instance, replace the instance name with a port number (for non-dynamic ported named instances)
$newspn.Port = $port
$newspn.DynamicPort = $false

if ($newspn.InstanceName -eq "MSSQLSERVER") {
$newspn.RequiredSPN = $newspn.RequiredSPN + ":" + $port
} else {
$newspn.RequiredSPN = $newspn.RequiredSPN.Replace($newSPN.InstanceName, $newspn.Port)
}

}

$spns += $newspn
}
}
$spns
}

Write-Verbose "Attempting to connect to SQL WMI on remote computer"
if ($Credential)
{
try
{
$spns = Invoke-ManagedComputerCommand -ComputerName $ipaddr -ScriptBlock $Scriptblock -ArgumentList $computer, $hostentry, $domain -Credential $Credential -ErrorAction Stop
}
catch
{
Write-Verbose "Couldn't connect to $ipaddr with credential. Using without credentials."
$spns = Invoke-ManagedComputerCommand -ComputerName $ipaddr -ScriptBlock $Scriptblock -ArgumentList $computer, $hostentry, $domain
}
}
else
{
$spns = Invoke-ManagedComputerCommand -ComputerName $ipaddr -ScriptBlock $Scriptblock -ArgumentList $computer, $hostentry, $domain
}


#Now query AD for each required SPN
ForEach ($spn in $spns)
{
$DN = "DC=" + $domain -Replace ("\.", ',DC=')
$LDAP = "LDAP://$DN"
$root = [ADSI]$LDAP
$ADObject = New-Object System.DirectoryServices.DirectorySearcher
$ADObject.SearchRoot = $root

$serviceAccount = $spn.InstanceServiceAccount

if ($serviceaccount -like "*\*")
{
Write-Debug "Account provided in in domain\user format. Stripping domain values."
$serviceaccount = ($serviceaccount.split("\"))[1]
}
if ($serviceaccount -like "*@*")
{
Write-Debug "Account provided in in user@domain format. Stripping domain values."
$serviceaccount = ($serviceaccount.split("@"))[0]
}

$ADObject.Filter = $("(&(samAccountName={0}))" -f $serviceaccount)

try
{
$results = $ADObject.FindAll()
}
catch
{
Write-Warning "AD lookup failure. This may be because the hostname ($computer) was not resolvable within the domain ($domain) or the SQL Server service account ($serviceaccount) couldn't be found in domain."
continue
}

if ($results.Count -gt 0)
{
if ($results.Properties.serviceprincipalname -contains $spn.RequiredSPN)
{
$spn.IsSet = $true
}
}

if (!$spn.IsSet -and $spn.TcpEnabled)
{
$spn.Error = "SPN missing"
}

$spn | Select-DefaultView -ExcludeProperty Credential, DomainName
}
}
}
}