0

Good morning everyone.

I found this script on the InterWeb's which works phenominal. HOWEVER... No matter what I try, and where I put it, I can't seem to get it to do the results to an out-file.

What the hell am I doing wrong, and where does the variable need to go?

# 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 ""
}
  • 1
    `Write-Host` outputs to the _Information Stream_, no need to use it if you want to output to the _Success Stream_ (aka Standard Output). See: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_output_streams?view=powershell-7.2 – Santiago Squarzon Dec 20 '21 at 16:26
  • **Remove** all the `Write-host` commands and pipe the results of the `foreach` to a file: `foreach( $result in $results ) { ... } |Set-Content .\output.txt – iRon Dec 20 '21 at 16:44
  • I definitely have quite a bit of PS learning to do. I tried removing all the `Write-Host` commands and replace like you said, and it was no bueno. – Charles Waters Dec 21 '21 at 17:47

0 Answers0