Posted in
Windows Powershell,
Windows Server |
No Comment | 4,567 views | 09/10/2013 16:07
You can find duplicated SPNs in your AD with this PowerShell script. That will output into txt file and console.
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
| $ADObjects = Get-ADObject -Filter "objectClass -eq 'user' -and objectClass -eq 'computer'" -Properties "samaccountname","serviceprincipalname"
$Array = New-Object System.Collections.ArrayList
$Array.Clear();
foreach ($ADObject in $ADObjects)
{
$SamAccountName = $ADObject.SamAccountName
$ServicePrincipalName = $ADObject.ServicePrincipalName
foreach ($SPN in $ServicePrincipalName)
{
$ReferenceObject = "$SamAccountName;$SPN"
if ($Array -like "*$SPN")
{
$Matched = $Array -like "*$SPN"
foreach ($Match in $Matched)
{
$MatchSAM = $Match.Split(";")[0]
if ($MatchSAM -ne $SamAccountName)
{
$Value = "$ReferenceObject%$Match"
Write-Host $Value
Add-Content -Value $Value -Path duplicated.txt
}
}
}
else
{
$Array.Add("$ReferenceObject")
}
}
} |
$ADObjects = Get-ADObject -Filter "objectClass -eq 'user' -and objectClass -eq 'computer'" -Properties "samaccountname","serviceprincipalname"
$Array = New-Object System.Collections.ArrayList
$Array.Clear();
foreach ($ADObject in $ADObjects)
{
$SamAccountName = $ADObject.SamAccountName
$ServicePrincipalName = $ADObject.ServicePrincipalName
foreach ($SPN in $ServicePrincipalName)
{
$ReferenceObject = "$SamAccountName;$SPN"
if ($Array -like "*$SPN")
{
$Matched = $Array -like "*$SPN"
foreach ($Match in $Matched)
{
$MatchSAM = $Match.Split(";")[0]
if ($MatchSAM -ne $SamAccountName)
{
$Value = "$ReferenceObject%$Match"
Write-Host $Value
Add-Content -Value $Value -Path duplicated.txt
}
}
}
else
{
$Array.Add("$ReferenceObject")
}
}
}
You should split duplicated objects by %.