List registered SPNs in Active Directory: pimped

This will go and poll all your registered SPNs in Active Directory and write them to a file. It accepts Debug, Log_Dir and Log_FileName as parameters.

Source: http://social.technet.microsoft.com/wiki/contents/articles/18996.list-all-spns-used-in-your-active-directory.aspx

  1<#
  2	.Synopsis
  3	Go and poll all your SPNs registered in Active Directory and write them to a file
  4
  5	.Description
  6	Go and poll all your SPNs registered in Active Directory and write them to a file.
  7
  8	.Author
  9	Harold Preyers
 10
 11	.Parameter Debug
 12	The Debug parameter shows output to the screen.
 13
 14	.Parameter Log_Dir
 15	Supply your own LogFile location.
 16
 17	.Parameter Log_FileName
 18	Supply your own LogFile location. Don't add a file extension, the script will construct a filename based on the time of launch and add a .log extension
 19
 20	.Example
 21	# Start SPN_List.ps1 without debug output based on the default log file location
 22	./SPN_List.ps1
 23
 24	.Example
 25	# Start storage vmotions with debug output based on the default log file location
 26	./SPN_List.ps1 -Debug $true 
 27
 28	.Example
 29	# Start storage vmotions with debug output based on custom log file location
 30	./SPN_List.ps1 -Debug $true -Log_Dir "C:\Scripts\SPN_List\Logs" -Log_FileName SPN_list 
 31
 32#>
 33
 34# Accept parameters
 35[cmdletBinding()]
 36	Param (
 37		[Bool]$Debug = $false				# Write output to console
 38		[string]$Log_Dir,					# Log Directory location
 39		[string]$Log_FileName,				# Log Filename
 40	)
 41
 42#Variables
 43$Break = "-------------------------"
 44
 45# Logging
 46$Log_Dir="C:\Scripts\Storage vMotion\Logs"
 47$Log_FileName="svMotion_log"
 48$Log_Breaker="##########################"
 49$global:File=""
 50
 51# Create the necessary directories if necessary
 52If(!(Test-Path -Path $Log_Dir )){
 53	New-Item -ItemType directory -Path $Log_Dir
 54}
 55
 56Function Initialize {
 57	# Logging parameters
 58	$Date = (get-date).tostring("yyyyMMdd_HHmmss")
 59	$global:File = $LogDir + "\" + $Log_FileName + "_" + $Date + ".log"
 60	If ($Debug) {Write-Host "The filename is: $global:File"}
 61
 62	# Initialize log
 63	$Log_Breaker | Out-File "$global:File" -Append
 64	" LogFile:  $global:File" | Out-File "$global:File" -Append
 65	" LogDate:  $Date" | Out-File "$global:File" -Append
 66	" CSV File: $CSV_File" | Out-File "$global:File" -Append
 67	$Log_Breaker | Out-File "$global:File" -Append
 68	Add-Content -Path c:\temp\SPN_List.txt -Value "`n"
 69}
 70
 71#Set Search
 72cls
 73$search = New-Object DirectoryServices.DirectorySearcher([ADSI]“”)
 74$search.filter = (servicePrincipalName=*)
 75$Results = $search.Findall()
 76
 77#list results
 78Foreach($Result in $Results)
 79{
 80	$userEntry = $result.GetDirectoryEntry()
 81
 82	$Output = "Object Name = " + $userEntry.name
 83	If ($Debug) {Write-Host $Output -backgroundcolor "yellow" -foregroundcolor "black"}
 84	$Output | Out-File c:\temp\SPN_List.txt -Append
 85	$Break | Out-File c:\temp\SPN_List.txt -Append
 86
 87	$Output = "DN = " + $userEntry.distinguishedName
 88	If ($Debug) {Write-host $Output}
 89	$Output | Out-File c:\temp\SPN_List.txt -Append
 90
 91	$Output = "Object Cat. = " + $userEntry.objectCategory
 92	If ($Debug) {Write-host $Output}
 93	$Output | Out-File c:\temp\SPN_List.txt -Append
 94	$Break | Out-File c:\temp\SPN_List.txt -Append
 95
 96	$Output = "servicePrincipalNames"
 97	If ($Debug) {Write-host $Output}
 98	$Output | Out-File c:\temp\SPN_List.txt -Append
 99	$Break | Out-File c:\temp\SPN_List.txt -Append
100
101	$i=1
102
103	foreach($SPN in $userEntry.servicePrincipalName) {
104		If (($i).tostring().length -le 1) {
105			$preZero = "0"
106		}
107		Else {
108			$preZero = ""
109		}
110		$Output = "SPN(" + $preZero + $i + ") = " + $SPN
111		If ($Debug) {Write-host "SPN(" $preZero$i ") = " $SPN}
112		$Output | Out-File c:\temp\SPN_List.txt -Append
113		$i+=1
114	}
115	If ($Debug) {Write-host ""}
116	$Break | Out-File c:\temp\SPN_List.txt -Append
117	Add-Content -Path c:\temp\SPN_List.txt -Value "`n"
118}</pre>
119
120&nbsp;