AD query results change when put into custom object

by sgribbin at 2012-08-16 13:13:24

I am trying to create a script that will query AD and return account information. We run a script at logon/logoff that populates the computer description field with {$username “logged on/off at” $date $time}. My script will take a list of user names, return the computer account properties based on the username in the description field and then build a custom object with these results. The script runs fine until I put the data into the custom object. The CN and Description properties turn into an LDAP syntax ( CN=xxxx,OU=xxxxx). This doesn’t happen when I remove the custom object section and just return the result for one username. Any ideas how I can fix this?

Import-Module ActiveDirectory

$Items = get-content C:\ps\input\ImpromedUsers.txt
Foreach ($Item in $Items)
$Comps = Get-ADComputer -LDAPFilter “(Description=$($Item)*)” -properties *

Foreach ($Comp in $Comps)
$PSO = New-Object psobject
$PSO | Add-Member -MemberType NoteProperty -Name User -Value “$Item”
$PSO | Add-Member -MemberType NoteProperty -Name ComputerName -Value “$Comp.Name”
$PSO | Add-Member -MemberType NoteProperty -Name Description -Value “$Comp.Description”
$UserComp += $PSO
$UserComp | FT User,ComputerName,Description

Any help would be much appreciated.
by poshoholic at 2012-08-16 13:59:55
You simply need to remove your double quotes around $comp.Name and $comp.Description. Otherwise PowerShell simply converts the $comp variable to a string first and then appends “.Name” or “.Description”, respectively, to that string.

If you want to keep the quotes, then your other option is to use a subexpression to instruct PowerShell to evaluate that first and then put the result in the string, like “$($comp.Name)” or “$($comp.Description)”.
by RichardSiddaway at 2012-08-17 08:46:34
any reason why you are using Add-member instead of defining the properties and values in New-object?