https://www.saotn.org/list-spns-used-active-directory/
List all SPNs used in your Active Directory
Windows Server system administrator & enthusiast.
There are a lot of hints & tips out there for troubleshooting SPNs, or Service Principal Names. Listing duplicate SPNs is fairly easy, just use setspn -X
on your command-line and you’ll find out. But how do you find out which
SPNs are used for which users and computers are used for this?
An SPN or Service Principal Name is a unique identity for a service, mapped with a specific account (mostly service account). Using an SPN, you can create multiple aliases for a service mapped with an Active Directory domain account.
SetSPN command-line
To set, list or delete the SPN, we use an in-built command line tool SETSPN (setspn.exe
) provided by Microsoft.
Quite
some scripts assume you’re looking for a specific SPN (HTTP/…), a
specific user, or a specific computer. For example, you can use setspn
to find (query) Service Principal Names (SPNs) linked to a certain computer:
setspn.exe -L <ServerName>
Code language: PowerShell (powershell)
Or you can use setspn
to find (query) SPNs linked to a certain user account:
setspn.exe -L <domain\user>
Code language: PowerShell (powershell)
And now you need a general script to list all SPNs, for all users and all computers…
Nice to know fact, Service Principal Names (SPNs) are set as an attribute on the user or computer accounts. That makes it fairly ease to query for that attribute. And modern admins do PowerShell, right?
List SPNs using Powershell
So… Save the following code into a new PowerShell .ps1
file and run it in your domain. It will query and list the Service Principal Names – SPNs.
# Source / credit:
# https://social.technet.microsoft.com/wiki/contents/articles/18996.active-directory-powershell-script-to-list-all-spns-used.aspx
cls
$search = New-Object DirectoryServices.DirectorySearcher([ADSI]"")
$search.filter = "(servicePrincipalName=*)"
## You can use this to filter for OU's:
## $results = $search.Findall() | ?{ $_.path -like '*OU=whatever,DC=whatever,DC=whatever*' }
$results = $search.Findall()
foreach( $result in $results ) {
$userEntry = $result.GetDirectoryEntry()
Write-host "Object Name = " $userEntry.name -backgroundcolor "yellow" -foregroundcolor "black"
Write-host "DN = " $userEntry.distinguishedName
Write-host "Object Cat. = " $userEntry.objectCategory
Write-host "servicePrincipalNames"
$i=1
foreach( $SPN in $userEntry.servicePrincipalName ) {
Write-host "SPN ${i} =$SPN"
$i+=1
}
Write-host ""
}
Code language: PowerShell (powershell)
Or use dsquery
on your CMD.exe command-line:
dsquery * "ou=domain controllers,dc=yourdomain,dc=com" -filter "(&(objectcategory=computer)
(servicePrincipalName=*))" -attr distinguishedName servicePrincipalName > spns.txt
Code language: PowerShell (powershell)
This is a valuable script and information reference for your own documentation.
No comments:
Post a Comment
Note: only a member of this blog may post a comment.