Advertisement
private775

PS: [ADSI] Get user SPNs

May 3rd, 2018
463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function get-AdUserSpns {
  2.     Param(
  3.         [string]$samAccountName = $env:USERNAME
  4.     )
  5.     $search = New-Object DirectoryServices.DirectorySearcher([ADSI]"")
  6.     $search.filter = "(&(samAccountType=805306368)(samAccountName=$($samAccountName)))"
  7.     $results = $search.Findall()
  8.  
  9.     #list results
  10.     foreach($result in $results)
  11.     {
  12.            $userEntry = $result.GetDirectoryEntry()
  13.            Write-host "Object Name    = " $userEntry.name -backgroundcolor "yellow" -foregroundcolor "black"
  14.            Write-host "samAccountName = "  $userEntry.samAccountName
  15.            Write-host "DN             = "  $userEntry.distinguishedName
  16.            Write-host "Object Cat.    = "  $userEntry.objectCategory
  17.            if($userEntry.servicePrincipalName.Count -gt 0){
  18.                Write-host -BackgroundColor DarkGreen -ForegroundColor Yellow "Found servicePrincipalNames:"
  19.                $i=1
  20.                foreach($SPN in $userEntry.servicePrincipalName)
  21.                {
  22.                    Write-host "SPN($($i.ToString('D3')))       = $($SPN)"
  23.                    $i+=1
  24.                }
  25.            } else {
  26.                   Write-Host -BackgroundColor DarkRed -ForegroundColor Yellow "No SPNs for a user"
  27.            }
  28.            Write-host ""
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement