powershellcsvpowershell-1.0directorysearcher

Trying to Pull list of computers from AD via powershell


So I'm trying to pull a list of computers from AD into a .csv file. I'm trying to pull the cn, description, distinguishedName, operatingSystem, whenCreated, and whenChanged fields using powershell.

The code I'm using is this:

# Start of script

Cls

$DomainRootPath='LDAP://DC=<name>,DC=ORG'

$adsearch = New-Object  DirectoryServices.DirectorySearcher([adsi]$DomainRootPath)
$adsearch.PageSize = 1000
$adsearch.filter = ("(objectCategory=Computer)")

$adsearch.PropertiesToLoad.AddRange(@("cn"))
$adsearch.PropertiesToLoad.AddRange(@("description"))
$adsearch.PropertiesToLoad.AddRange(@("distinguishedName"))
$adsearch.PropertiesToLoad.AddRange(@("operatingSystem"))
$adsearch.PropertiesToLoad.AddRange(@("whenCreated"))
$adsearch.PropertiesToLoad.AddRange(@("whenChanged"))


$computers = $adsearch.findall()

$computers.Count

$report = @()

foreach ($objResult in $computers)
{

$objItem = $objResult.Properties

$temp = New-Object PSObject

$temp | Add-Member NoteProperty cn $($objitem.cn)
$temp | Add-Member NoteProperty description $($objitem.description)
$temp | Add-Member NoteProperty distinguishedName $($objitem.distinguishedName)
$temp | Add-Member NoteProperty operatingSystem $($objitem.operatingSystem)
$temp | Add-Member NoteProperty whenCreated $($objitem.whenCreated)
$temp | Add-Member NoteProperty whenChanged $($objitem.whenChanged)


$report += $temp
}

$csvfile="AD-All-Computers.csv"

$report | export-csv -notypeinformation $csvfile
"Wrote file for All Computers"

Now This code does create the .csv with the correct fields for the column names. However it only pulls the cn and description of each computer in the directory. All of the other fields are blank when it populates the .csv

Not sure what is going wrong in the code as I get no error any help is much appreciated.

On a side note I am running Windows 2008 R2 server and unable to run powershell 2.0 cmdlets in my environment due to certain systems preventing us to be able to confgiure AD to do so.


Solution

  • It's a funny odd frustrating problem, but the issue is because you are using capital letters (camelCase) when referencing the properties, when you need to reference them with all lower case letters.

    i.e. instead of $objitem.distinguishedName it should be: $objitem.distinguishedname

    So, replace your foreach block with this, and you should be getting all those properties:

    $report = @()
    
    foreach ($objResult in $computers)
    {
    
    $objItem = $objResult.Properties
    
    $temp = New-Object PSObject
    
    $temp | Add-Member NoteProperty cn $($objitem.cn)
    $temp | Add-Member NoteProperty description $($objitem.description)
    $temp | Add-Member NoteProperty distinguishedName $($objitem.distinguishedname)
    $temp | Add-Member NoteProperty operatingSystem $($objitem.operatingsystem)
    $temp | Add-Member NoteProperty whenCreated $($objitem.whencreated)
    $temp | Add-Member NoteProperty whenChanged $($objitem.whenchanged)
    
    
    $report += $temp
    }