Export isn't showing PC Name

Welcome Forums General PowerShell Q&A Export isn't showing PC Name

This topic contains 0 replies, has 1 voice, and was last updated by  bmiller84 1 month ago.

  • Author
  • #172207

    Topics: 2
    Replies: 1
    Points: 11
    Rank: Member

    Hey Everyone,

    New to Powershell scripting and could use a little help. I modified this script to pull the local admins from all PC's in an OU and generate a CSV. The script runs fine, the output isn't exactly what i'm looking for and not sure how to fix. Below is the script and  the CSV output (I highlighted both lines in bold red that I need help with). Where it has "System.DirectoryServices.SearchResult" I would like to have the PC DNS name. Thank you in advance for all of your help!



    Import-Module ActiveDirectory
    $Searcher = New-Object DirectoryServices.DirectorySearcher([ADSI]"")
    $Searcher.SearchRoot = 'LDAP://OU=Staging,OU=Computers,OU=COMPANY,DC=corp,DC=local'
    $Searcher.Filter = "(objectClass=computer)"
    $Computers = ($Searcher.Findall())

    $Results = @()
    md C:\All_Local_Admins

    Foreach ($Computer in $Computers){
    write-host $Name
    $members =[ADSI]"WinNT://$Name/Administrators"
    $members = @($members.psbase.Invoke("Members"))
    $members | foreach {
    $LocalAdmins = $_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null) # Create a new object for the purpose of exporting as a CSV
    $pubObject = new-object PSObject
    $pubObject | add-member -membertype NoteProperty -name "Server" -Value $Name
    $pubObject | add-member -membertype NoteProperty -name "Computer" -Value $Computer
    $pubObject | add-member -membertype NoteProperty -name "Administrators" -Value $LocalAdmins

    # Append this iteration of our for loop to our results array.
    $Results += $pubObject

    $Results | Export-Csv -Path "C:\All_Local_Admins\ServerLocalAdmins.csv" -NoTypeInformation
    $Results = $Null



    Server Computer Administrators
    System.DirectoryServices.PropertyValueCollection System.DirectoryServices.SearchResult Administrator

You must be logged in to reply to this topic.