-
Notifications
You must be signed in to change notification settings - Fork 522
/
ActiveDirectorySPN.psm1
1434 lines (1206 loc) · 39.9 KB
/
ActiveDirectorySPN.psm1
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Set-StrictMode -Version Latest
#region function ConvertFrom-StringSpn
function ConvertFrom-StringSpn
{
<#
.SYNOPSIS
ConvertFrom-StringSpn takes a typical SPN string in the form serviceclass/host:port/servicename
and parses out the service class, host, port and service name to create a single object
with each SPN attribute as object properties.
.DESCRIPTION
A detailed description of the ConvertFrom-StringSpn function.
.PARAMETER SpnString
The SPN string that needs to be split into an object
.EXAMPLE
PS> ConvertFrom-StringSpn -SpnString 'serviceclass/host:389/servicename'
This example parses out the string 'serviceclass/host:389/servicename' to create a single object
with the properties 'serviceclass','host','port' and 'servicename'.
.OUTPUTS
System.Management.Automation.PSCustomObject. Convert-SpnStringToObject returns a PSCustomobject
with a property representing each piece of that makes up the SPN string.
.NOTES
Created on:6/7/15
Created by:Adam Bertram
.INPUTS
None. You cannot pipe objects to ConvertFrom-StringSpn.
#>
[CmdletBinding()]
[OutputType([System.Management.Automation.PSCustomObject])]
param
(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string[]]$SpnString
)
try
{
foreach ($spn in $SpnString)
{
$output = @{
ServiceClass = $null
HostName = $null
Port = $null
ServiceName = $null
}
$dashSplit = $spn.Split('/')
$output.ServiceClass = $dashSplit[0]
## The SPN has a port
if ($spn -match ':')
{
$output.Port = $spn.Split(':')[1].Split('/')[0]
}
## The SPN has a service name
if ($spn -like '*/*/*')
{
$output.ServiceName = $dashSplit[$dashSplit.Length - 1]
}
$output.HostName = $spn.Split(':')[0].Split('/')[1]
[pscustomobject]$output
}
}
catch
{
Write-Error $_.Exception.Message
}
}
#endregion function ConvertFrom-StringSpn
#region function New-SpnString
function New-SpnString
{
<#
.SYNOPSIS
New-SpnString is used to build SPN strings from object properties. SPN strings have been separated into
their respective properties in this module to ease administration. This functions converts object
properties and concatenates them all into a single string.
.DESCRIPTION
A detailed description of the New-SpnString function.
.PARAMETER ServiceClass
The service class attribute that's contained inside the SPN string. This is a mandatory field
because a SPN must have a service class attribute.
.PARAMETER HostName
The host name attribute that's contained inside the SPN string. This is a mandatory field
because a SPN must have a host name attribute.
.PARAMETER Port
If a non-standard port is in the SPN string, specify that here. This is not usually specified in
a SPN but is available if you need to match a SPN with a non-standard port.
.PARAMETER ServiceName
If a non-standard service name is in the SPN string to remove, specify that here. This is not usually
specified in a SPN but is available if you need to match a SPN with a non-standard service name
.EXAMPLE
PS> New-SpnString -ServiceClass 'someserviceclass' -HostName 'somehost'
This example concatenates the service class 'someserviceclass' and host name 'somehost' together to
output 'someserviceclass/somehost'
.OUTPUTS
System.String. New-SpnString outputs a single SPN string.
.NOTES
Created on:6/7/15
Created by:Adam Bertram
.INPUTS
None. You cannot pipe objects to New-SpnString.
#>
[CmdletBinding()]
[OutputType([string])]
param
(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$ServiceClass,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$HostName,
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$Port,
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$ServiceName
)
try
{
$spnString = "$ServiceClass/$HostName"
if ($PSBoundParameters.ContainsKey('Port'))
{
$spnString += "`:$Port"
}
if ($PSBoundParameters.ContainsKey('ServiceName'))
{
$spnString += "/$ServiceName"
}
$spnString
}
catch
{
Write-Error $_.Exception.Message
}
}
#endregion function New-SpnString
#region function Get-ADSpn
function Get-ADSpn
{
<#
.SYNOPSIS
The Get-ADSpn function will query the current Active Directory domain for all SPNs associated with
a particular object.
.DESCRIPTION
A detailed description of the Get-ADSpn function.
.PARAMETER DomainName
Specifies a fully qualified domain name (FQDN) for an Active Directory domain.
Example format: -Domain "Domain01.Corp.Contoso.com"
.PARAMETER ObjectType
The kind of AD object that the SPN will be created under. This can either be computer or user.
.PARAMETER ObjectValue
The value of the object type.
.PARAMETER Credential
A PSCredential object that will be used to perform the AD query. This is the credential that will be used
for the Get-User cmdlet.
.EXAMPLE
PS> Get-ADSpn -ObjectType 'User' -UserName MYUser
This example will query Active Directory for all SPNs attached to the user object with the SamAccountName
'MYUser'. When found, it will output one or more PS custom objects representing all the SPNs the user
account possesses.
.OUTPUTS
System.Management.Automation.PSCustomObject. Get-ADSpn can return nothing, one or multiple objects depending
on if it finds any.
.NOTES
Created on:6/7/15
Created by:Adam Bertram
.INPUTS
None. Get-ADSpn does not accept pipeline input.
#>
[CmdletBinding()]
[OutputType([System.Management.Automation.PSCustomObject])]
param
(
[Parameter(Mandatory)]
[ValidateSet('Computer', 'User')]
[ValidateNotNullOrEmpty()]
[string]$ObjectType,
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$ObjectValue,
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$DomainName,
[Parameter()]
[pscredential]$Credential
)
$getFunction = 'Get-AD{0}' -f $ObjectType
if (($ObjectType -eq 'Computer') -and (-not ($ObjectValue.EndsWith('$'))))
{
$ObjectValue = $ObjectValue + '$'
}
$getParams = @{
Properties = @('CanonicalName', 'ServicePrincipalNames')
}
if ($PSBoundParameters.ContainsKey('ObjectValue'))
{
$getParams.Filter = "SamAccountName -eq '$ObjectValue'"
}
else
{
$getParams.Filter = "ServicePrincipalNames -like '*'"
}
if ($PSBoundParameters.ContainsKey('DomainName'))
{
$getParams.Server = $DomainName
}
if ($PSBoundParameters.ContainsKey('Credential'))
{
$getParams.Credential = $Credential
}
&$getFunction @getParams | Where-Object ServicePrincipalNames | ForEach-Object {
$spnObjects = ConvertFrom-StringSpn $_.ServicePrincipalNames
foreach ($spnObject in $spnObjects)
{
$addMemberParams = @{
MemberType = 'NoteProperty'
}
$DomainName = $_.CanonicalName.Split('/')[0]
$spnObject | Add-Member @addMemberParams -Name SamAccountName -Value $_.samAccountName
$spnObject | Add-Member @addMemberParams -Name DomainName -Value $DomainName
$spnObject
}
}
}
#endregion function Get-ADSpn
#region function New-ADSpn
function New-ADSpn
{
<#
.SYNOPSIS
The New-ADSpn function is designed to create new SPNs in Active Directory. New-ADSpn will
first perform a test to see if the SPN is already in the domain. If not, it will then find the object in
Active Directory. If found, it will then attempt to create the SPN. After creation, New-ADSpn
performs a test to ensure the SPN was successfully added.
The New-ADSpn function is the function that both the New-ADUserSPn and New-ADComputerSpn function calls.
.DESCRIPTION
A detailed description of the New-ADSpn function.
.PARAMETER ObjectType
The type of AD object that the SPN will apply to. This can either be 'Computer' or 'User'
.PARAMETER ObjectValue
The value of the object type.
.PARAMETER ServiceClass
The service class attribute that's contained inside the SPN string. This is a mandatory field
because a SPN must have a service class attribute.
.PARAMETER HostName
The host name attribute that's contained inside the SPN string. This is a mandatory field
because a SPN must have a host name attribute.
.PARAMETER Port
If a non-standard port is in the SPN string, specify that here. This is not usually specified in
a SPN but is available if you need to match a SPN with a non-standard port.
.PARAMETER ServiceName
If a non-standard service name is in the SPN string to remove, specify that here. This is not usually
specified in a SPN but is available if you need to match a SPN with a non-standard service name
.PARAMETER DomainName
Specifies a fully qualified domain name (FQDN) for an Active Directory domain.
Example format: -Domain "Domain01.Corp.Contoso.com"
.PARAMETER Credential
A PSCredential object that will be used to perform the AD query. This is the credential that will be used
for the Set-AdUser cmdlet.
.EXAMPLE
PS> New-ADSpn -ObjectType User -ObjectValue MyUsername -ServiceClass ldap -HostName 'SomeHost'
This example will attempt to add a SPN to the 'MyUsername' user account with a service class
attribute of 'ldap' and a hostname attribute of 'SomeHost'.
.OUTPUTS
Null. This function does not return any output if successful.
.NOTES
Created on:6/7/15
Created by:Adam Bertram
.INPUTS
Any kind of objects can be piped to this function. The function will match the UserName, ServiceClass,
HostName, Port and ServiceName properties on the incoming object.
#>
[CmdletBinding()]
[OutputType()]
param
(
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[ValidateSet('Computer', 'User')]
[ValidateNotNullOrEmpty()]
[string]$ObjectType,
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[string]$ObjectValue,
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[string]$ServiceClass,
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[string]$HostName,
[Parameter(ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[string]$Port,
[Parameter(ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[string]$ServiceName,
[Parameter(ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[string]$DomainName,
[Parameter()]
[pscredential]$Credential
)
process
{
$getFunction = 'Get-AD{0}' -f $ObjectType
$setFunction = 'Set-AD{0}' -f $ObjectType
$testSpnParams = @{
ServiceClass = $ServiceClass
HostName = $HostName
}
if ($PSBoundParameters.ContainsKey('Port'))
{
$testSpnParams.Port = $Port
}
if ($PSBoundParameters.ContainsKey('ServiceName'))
{
$testSpnParams.ServiceName = $ServiceName
}
if ($PSBoundParameters.ContainsKey('DomainName'))
{
$testSpnParams.DomainName = $DomainName
}
if ($PSBoundParameters.ContainsKey('Credential'))
{
$testSpnParams.Credential = $Credential
}
$spnParams = @{ }
$testSpnParams.GetEnumerator() | Where-Object Key -notin @('Credential', 'DomainName', 'ObjectType', 'ObjectValue') | ForEach-Object {
$spnParams[$_.Key] = $_.Value
}
$spnString = New-SpnString @spnParams
if (Test-ADSpn @testSpnParams)
{
throw "An existing SPN with string [$spnString] already exists"
}
$getParams = @{
Identity = $ObjectValue
}
if ($PSBoundParameters.ContainsKey('DomainName'))
{
$getParams.Server = $DomainName
}
if ($PSBoundParameters.ContainsKey('Credential'))
{
$getParams.Credential = $Credential
}
$account = &$getFunction @getParams
if (-not $account)
{
throw "The [$ObjectType] account [$ObjectValue] was not found in Active Directory"
}
else
{
Write-Verbose -Message "Adding the SPN string of [$spnString] to [$ObjectType] account [$ObjectValue]"
$setParams = @{
Identity = $account
ServicePrincipalNames = @{
Add = $spnString
}
}
if ($PSBoundParameters.ContainsKey('Credential'))
{
$setParams.Credential = $Credential
}
if ($PSBoundParameters.ContainsKey('DomainName'))
{
$setParams.Server = $DomainName
}
&$setFunction @setParams
}
if (-not (Test-ADSpn @testSpnParams))
{
throw "Failed to create SPN string [$spnString]"
}
else
{
Write-Verbose -Message "Successfully created SPN string [$spnString]"
}
}
}
#endregion function New-ADSpn
#region function Remove-ADSpn
function Remove-ADSpn
{
<#
.SYNOPSIS
The Remove-ADSpn function is designed to remove SPNs from Active Directory objects. Remove-ADSpn will
first find the object in Active Directory. If found, it will then find all SPNs associated with the object.
If the specified SPN is found on the object it will attempt to remove the SPN. After removal, Remove-ADSpn
performs a test to ensure the SPN was removed.
.DESCRIPTION
A detailed description of the Remove-ADSpn function.
.PARAMETER ObjectType
The kind of AD object that the SPN will be created under. This can either be computer or user.
.PARAMETER ObjectValue
The value of the object type.
.PARAMETER ServiceClass
The service class attribute that's contained inside the SPN string. This is a mandatory field
because a SPN must have a service class attribute.
.PARAMETER HostName
The host name attribute that's contained inside the SPN string. This is a mandatory field
because a SPN must have a host name attribute.
.PARAMETER Port
If a non-standard port is in the SPN string, specify that here. This is not usually specified in
a SPN but is available if you need to match a SPN with a non-standard port.
.PARAMETER ServiceName
If a non-standard service name is in the SPN string to remove, specify that here. This is not usually
specified in a SPN but is available if you need to match a SPN with a non-standard service name.
.PARAMETER DomainName
Specifies a fully qualified domain name (FQDN) for an Active Directory domain.
Example format: -Domain "Domain01.Corp.Contoso.com"
.PARAMETER Credential
A PSCredential object that will be used to perform the AD query. This is the credential that will be used
for the Set-AdUser cmdlet.
.EXAMPLE
PS> Remove-ADSpn -ObjectType User -ObjectValue MyUsername -ServiceClass ldap -HostName 'SomeHost'
This example will attempt to remove all SPNs associated with the 'MyUsername' user account that have a service class
attribute of 'ldap' and a hostname attribute of 'SomeHost'. If no SPN is found, the function will simply write
verbose output letting the user know this and will exit.
.OUTPUTS
Null. This function does not return any output if successful.
.NOTES
Created on:6/7/15
Created by:Adam Bertram
.INPUTS
Any kind of objects can be piped to this function. The function will match the UserName, ServiceClass,
HostName, Port and ServiceName properties on the incoming object.
#>
[CmdletBinding()]
[OutputType()]
param
(
[Parameter(Mandatory)]
[ValidateSet('Computer', 'User')]
[ValidateNotNullOrEmpty()]
[string]$ObjectType,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$ObjectValue,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$ServiceClass,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$HostName,
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$Port,
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$ServiceName,
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$DomainName,
[Parameter()]
[pscredential]$Credential
)
$getFunction = 'Get-AD{0}' -f $ObjectType
$setFunction = 'Set-AD{0}' -f $ObjectType
$testSpnParams = @{
ServiceClass = $ServiceClass
HostName = $HostName
}
if ($PSBoundParameters.ContainsKey('Port'))
{
$testSpnParams.Port = $Port
}
if ($PSBoundParameters.ContainsKey('ServiceName'))
{
$testSpnParams.ServiceName = $ServiceName
}
if ($PSBoundParameters.ContainsKey('DomainName'))
{
$testSpnParams.DomainName = $DomainName
}
if ($PSBoundParameters.ContainsKey('Credential'))
{
$testSpnParams.Credential = $Credential
}
if (Test-ADSpn @testSpnParams)
{
$spnString = "$ServiceClass/$HostName"
if ($Port)
{
$spnString += "`:$Port"
}
if ($ServiceName)
{
$spnString += "/$ServiceName"
}
Write-Verbose -Message "Found an existing SPN string for $ObjectType account [$ObjectValue] matching SPN string [$spnString]"
if (($ObjectType -eq 'Computer') -and (-not $ObjectValue.EndsWith('$')))
{
$samAccountName = "$ObjectValue{0}" -f '$'
}
else
{
$samAccountName = $ObjectValue
}
$getParams = @{
Identity = $samAccountName
Properties = 'ServicePrincipalNames'
}
if ($PSBoundParameters.ContainsKey('DomainName'))
{
$getParams.Server = $DomainName
}
if ($PSBoundParameters.ContainsKey('Credential'))
{
$getParams.Credential = $Credential
}
$account = &$getFunction @getParams
$setParams = @{
Identity = $samAccountName
ServicePrincipalNames = @{
Remove = $spnString
}
}
if ($PSBoundParameters.ContainsKey('DomainName'))
{
$setParams.Server = $DomainName
}
if ($PSBoundParameters.ContainsKey('Credential'))
{
$setParams.Credential = $Credential
}
&$setFunction @setParams
if (Test-ADSpn @testSpnParams)
{
throw "The SPN string [$spnString] was NOT successfully removed from [$ObjectType] account [$ObjectValue]"
}
else
{
Write-Verbose -Message "Successfully removed SPN [$spnString] from [$ObjectType] account [$ObjectValue]"
}
}
else
{
Write-Verbose "The SPN string [$spnString] already does not exist for the [$ObjectType] account [$ObjectValue]"
}
}
#endregion function Remove-ADSpn
#region function Test-ADSpn
function Test-ADSpn
{
<#
.SYNOPSIS
Test-ADSpn queries Active Directory and attempts to find existing SPN strings. If found, the function
will return $true. If not SPN string is found, it will return $false.
.DESCRIPTION
A detailed description of the Test-ADSpn function.
.PARAMETER ServiceClass
The service class attribute that's contained inside the SPN string. This is a mandatory field
because a SPN must have a service class attribute.
.PARAMETER HostName
The host name attribute that's contained inside the SPN string. This is a mandatory field
because a SPN must have a host name attribute.
.PARAMETER Port
If a non-standard port is in the SPN string, specify that here. This is not usually specified in
a SPN but is available if you need to match a SPN with a non-standard port.
.PARAMETER ServiceName
If a non-standard service name is in the SPN string to remove, specify that here. This is not usually
specified in a SPN but is available if you need to match a SPN with a non-standard service name
.PARAMETER DomainName
Specifies a fully qualified domain name (FQDN) for an Active Directory domain.
Example format: -Domain "Domain01.Corp.Contoso.com"
.PARAMETER Credential
A description of the Credential parameter.
.EXAMPLE
PS> Test-ADSpn -ServiceClass 'someserviceclass' -HostName 'somehostname'
This example queries Active Directory for SPN strings that contain a ServiceClass of 'someserviceclass' and
a host name of 'somehostname'. The SPN string that it will actually query is 'someserviceclass/somehostname'
.OUTPUTS
bool. Test-AdSpn outputs either boolean $true or $false
.NOTES
Created on:6/7/15
Created by:Adam Bertram
.INPUTS
None. You cannot pipe objects to Test-AdSpn.
#>
[CmdletBinding()]
[OutputType([bool])]
param
(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$ServiceClass,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$HostName,
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$Port,
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$ServiceName,
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$DomainName,
[Parameter()]
[pscredential]$Credential
)
$spnParams = @{ }
$PSBoundParameters.GetEnumerator() | Where-Object Key -notin @('Credential', 'DomainName') | ForEach-Object {
$spnParams[$_.Key] = $_.Value
}
$spnString = New-SpnString @spnParams
$adObjParams = @{
LDAPFilter = 'servicePrincipalName=*'
Properties = 'servicePrincipalName'
}
if ($PSBoundParameters.ContainsKey('DomainName'))
{
$adObjParams.Server = $DomainName
}
if ($PSBoundParameters.ContainsKey('Credential'))
{
$adObjParams.Credential = $Credential
}
if ((Get-ADObject @adObjParams).servicePrincipalName | Where-Object { $_ -eq $spnString })
{
$true
}
else
{
$false
}
}
#endregion function Test-ADSpn
#region function Get-ADComputerSpn
function Get-ADComputerSpn
{
<#
.SYNOPSIS
The Get-ADComputerSpn function will query the current Active Directory domain for all SPNs associated with
a particular computer object.
.DESCRIPTION
A detailed description of the Get-ADComputerSpn function.
.PARAMETER ComputerName
The samAccountName for the computer name that you'd like to query. If no ComputerName value is specified,
the function will output all SPNs associated with all computer accounts in the domain.
.PARAMETER Credential
A PSCredential object that will be used to perform the AD query. This is the credential that will be used
for the Get-Computer cmdlet.
.PARAMETER DomainName
Specifies a fully qualified domain name (FQDN) for an Active Directory domain.
Example format: -Domain "Domain01.Corp.Contoso.com"
.EXAMPLE
PS> Get-ADComputerSpn -ComputerName MYPC
This example will query Active Directory for all SPNs attached to the computer object with the SamAccountName
'MYPC'. When found, it will output one or more PS custom objects representing all the SPNs the computer
account possesses.
.OUTPUTS
System.Management.Automation.PSCustomObject. Get-ADComputerSpn can return nothing, one or multiple objects depending
on if it finds any.
.NOTES
Created on:6/7/15
Created by:Adam Bertram
.INPUTS
Strings are meant to be piped to this function. Simple strings representing the ComputerName parameter
can be piped to the function or objects with the ComputerName parameter.
#>
[CmdletBinding()]
[OutputType([System.Management.Automation.PSCustomObject])]
param
(
[Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[string]$ComputerName,
[Parameter(ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[string]$DomainName,
[Parameter()]
[pscredential]$Credential
)
process
{
try
{
$params = @{
ObjectType = 'Computer'
}
if ($PSBoundParameters.ContainsKey('ComputerName'))
{
$params.ObjectValue = $ComputerName
}
if ($PSBoundParameters.ContainsKey('DomainName'))
{
$params.DomainName = $DomainName
}
if ($PSBoundParameters.ContainsKey('Credential'))
{
$params.Credential = $Credential
}
Get-ADSpn @params | Select-Object *, @{ n = 'ComputerName'; e = { $_.SamAccountName.TrimEnd('$') } } -ExcludeProperty SamAccountName
}
catch
{
Write-Error $_.Exception.Message
}
}
}
#endregion function Get-ADComputerSpn
#region function New-ADComputerSpn
function New-ADComputerSpn
{
<#
.SYNOPSIS
The New-ADComputerSpn function is designed to create new SPNs in Active Directory computer objects. New-ADComputerSpn will
first perform a test to see if the SPN is already in the domain. If not, it will then find the computer object in
Active Directory. If found, it will then attempt to create the SPN. After creation, New-ADComputerSpn
performs a test to ensure the SPN was successfully added.
.DESCRIPTION
A detailed description of the New-ADComputerSpn function.
.PARAMETER ComputerName
The Active Directory computer name in which a SPN will be attempted to be created against.
.PARAMETER ServiceClass
The service class attribute that's contained inside the SPN string. This is a mandatory field
because a SPN must have a service class attribute.
.PARAMETER HostName
The host name attribute that's contained inside the SPN string. This is a mandatory field
because a SPN must have a host name attribute.
.PARAMETER Port
If a non-standard port is in the SPN string, specify that here. This is not usually specified in
a SPN but is available if you need to match a SPN with a non-standard port.
.PARAMETER ServiceName
If a non-standard service name is in the SPN string to remove, specify that here. This is not usually
specified in a SPN but is available if you need to match a SPN with a non-standard service name.
.PARAMETER DomainName
Specifies a fully qualified domain name (FQDN) for an Active Directory domain.
Example format: -Domain "Domain01.Corp.Contoso.com"
.PARAMETER Credential
A PSCredential object that will be used to perform the AD query. This is the credential that will be used
for the Set-AdUser cmdlet.
.EXAMPLE
PS> New-ADComputerSpn -ComputerName Mycomputername -ServiceClass ldap -HostName 'SomeHost'
This example will attempt to add a SPN to the 'Mycomputername' computer account with a service class
attribute of 'ldap' and a hostname attribute of 'SomeHost'.
.OUTPUTS
Null. This function does not return any output if successful.
.NOTES
Created on:6/7/15
Created by:Adam Bertram
.INPUTS
Any kind of objects can be piped to this function. The function will match the ComputerName, ServiceClass,
HostName, Port and ServiceName properties on the incoming object.
#>
[CmdletBinding(SupportsShouldProcess)]
[OutputType()]
param
(
[Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[string]$ComputerName,
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[string]$ServiceClass,
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[string]$HostName,
[Parameter(ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[string]$Port,
[Parameter(ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[string]$ServiceName,
[Parameter(ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[string]$DomainName,
[Parameter()]
[pscredential]$Credential
)
process
{
try
{
$params = @{
ObjectType = 'Computer'
ObjectValue = $ComputerName
ServiceClass = $ServiceClass
HostName = $HostName
}
if ($PSBoundParameters.ContainsKey('Port'))
{
$params.Port = $Port
}
if ($PSBoundParameters.ContainsKey('ServiceName'))
{
$params.ServiceName = $ServiceName
}
if ($PSBoundParameters.ContainsKey('DomainName'))
{
$params.DomainName = $DomainName
}
if ($PSBoundParameters.ContainsKey('Credential'))
{
$params.Credential = $Credential
}
if ($PSCmdlet.ShouldProcess($ComputerName, 'Create new SPN'))
{
New-ADSpn @params
}
}
catch
{
Write-Error $_.Exception.Message
}
}
}
#endregion function New-ADComputerSpn
#region function Remove-ADComputerSpn
function Remove-ADComputerSpn
{
<#
.SYNOPSIS
The Remove-ADComputerSpn function is designed to remove SPNs from Active Directory computer objects. Remove-ADComputerSpn will
first find the computer object in Active Directory. If found, it will then find all SPNs associated with the computer object.
If the specified SPN is found on the computer object it will attempt to remove the SPN. After removal, Remove-ADComputerSpn
performs a test to ensure the SPN was removed.
.DESCRIPTION
A detailed description of the Remove-ADComputerSpn function.
.PARAMETER ComputerName
The Active Directory computer name in which a SPN will be attempted to be removed from.
.PARAMETER ServiceClass
The service class attribute that's contained inside the SPN string. This is a mandatory field
because a SPN must have a service class attribute.
.PARAMETER HostName
The host name attribute that's contained inside the SPN string. This is a mandatory field
because a SPN must have a host name attribute.
.PARAMETER Port
If a non-standard port is in the SPN string, specify that here. This is not usually specified in
a SPN but is available if you need to match a SPN with a non-standard port.
.PARAMETER ServiceName
If a non-standard service name is in the SPN string to remove, specify that here. This is not usually
specified in a SPN but is available if you need to match a SPN with a non-standard service name.
.PARAMETER DomainName
Specifies a fully qualified domain name (FQDN) for an Active Directory domain.
Example format: -Domain "Domain01.Corp.Contoso.com"
.PARAMETER Credential
A PSCredential object that will be used to perform the AD query. This is the credential that will be used