1

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.

1 Answer 1

1

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
}
2
  • This worked perfectly and thank you for pointing that out for me. I'm still new to powershell and trying to learn as much as I can about it. Nov 27, 2013 at 17:14
  • You're welcome! Normally you don't have to worry about case sensitivity, but this is one of those odd times where it matters. Can you please mark the question as answered? Thank you.
    – HAL9256
    Nov 28, 2013 at 3:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.