powershellactive-directoryou

Powershell script to retrieve PC Membership from one OU


First, I have no formal training in Powershell, so most of what I write is adapted code or is relatively simple. I'm having problems retrieving either extended properties for a machine, or reorganizing the list of columns that is returned.

In my organization, a PC can be either a member of a the local OU that I'm privy to, or an enterprise OU that I have no rights to. A scan of my local OU Groups wouldn't give me all the results I'm looking for. I was able to find the answer to apply this logic to Users and Group memberships, but can't seem to get it working for PCs.

Import-Module ActiveDirectory
$OU = "OU=Computers,OU={ORG UNIT}"
$Path = "C:\{FILE LOC}\AD-groupPClist_$(get-date -f yyyyMMdd_HHmm).csv"

$PC = ForEach ($P in (Get-ADComputer -Filter * -SearchBase $OU))
{
$Machine = Get-ADComputer $P -Properties MemberOf
$Groups = ForEach ($Group in ($Machine.MemberOf))
{
    (Get-ADGroup $Group).Name
}
$Groups = $Groups | Sort
ForEach ($Group in $Groups)
{
    New-Object PSObject -Property @{
    Group = $Group
    PCName= $Machine.Name
    }
}
}
$PC | Export-CSV $Path -NoTypeInformation

When I try to add the DNSHostName column (for example), it appears as the first column in the results, even if it's listed third. In this case, if I added DNSHostName, I'd want the output file to be Group Name, PCName, DNSHostName (or some other attribute); but in that order.


Solution

  • I have made some modifications, but this should do the job.

    Import-Module ActiveDirectory
    $OU = "OU=Computers,OU={ORG UNIT}"
    $Path = "C:\{FILE LOC}\AD-groupPClist_$(get-date -f yyyyMMdd_HHmm).csv"
    
    $PC = ForEach ($P in (Get-ADComputer -Filter * -SearchBase $OU -Properties MemberOf)) {
        Foreach ($Group in $P.MemberOf) {
            [PSCustomObject]@{
                Group = (Get-ADGroup $Group).Name
                PCName = $P.Name
                DNSHostName = $P.DNSHostName
            }
        }
    }
    $PC | Sort-Object Group | Export-Csv $Path -NoTypeInformation
    

    Regarding the order of properties or CSV headers, the @{} instructs PowerShell that you are using hash table. The default behavior of a hash table is to be unordered. To get around this behavior, you need to create your hash table with the [ordered] attribute or just use the [pscustomobject] type accelerator when creating the object. The New-Object command alone is not immune to the unordered nature of a hash table. Since [PSCustomObject] is faster and does not have the ordering issue, it is best to just use that in favor of New-Object for custom objects.

    See below for a behavioral example of using New-Object with a hash table:

    # Unordered output example
    
    New-Object -TypeName psobject -Property `
         @{
           property3 = 'value3'
           property2 = 'value2'
           property1 = 'value1'
         }
    
    property1 property2 property3
    --------- --------- ---------
    value1    value2    value3
    
    # Example enforcing order
    
    New-Object -TypeName psobject -Property `
         ([ordered]@{
           property3 = 'value3'
           property2 = 'value2'
           property1 = 'value1'
         })
    
    property3 property2 property1
    --------- --------- ---------
    value3    value2    value1
    

    See About Hash Tables for more information regarding hash tables.