I've written a script that builds an array containing information about server objects in my AD. The script seems to be doing what I want, but the resulting objects have all their data enclosed in braces. I've tried using tostring() to convert them, but then the data just disappears. Cany anyone point out where I'm going wrong and/or what I need to do to fix it?
TIA,
Dan
Code:
$Servers = @()
$ADServers = New-Object System.DirectoryServices.DirectoryEntry("LDAP://dc=myDomain,dc=ca")$ADSearcher = New-Object System.DirectoryServices.DirectorySearcher($ADServers)$ADSearcher.SearchScope = "Subtree"$ADSearcher.Filter = ("(&(objectCategory=Computer)(operatingsystem=*Server*))")$SearchResult = $ADSearcher.FindAll()
foreach ($Result in $SearchResult){ $Server = $Result.Properties $CurrentServerObject = New-Object –TypeName PSObject –Property (@{ operatingsystemversion = $server.operatingsystemversion lastlogontimestamp = $server.lastlogontimestamp objectsid = $server.objectsid whencreated = $server.whencreated name = $server.name dnshostname = $server.dnshostname samaccounttype = $server.samaccounttype distinguishedname = $server.distinguishedname cn = $server.cn objectcategory = $server.objectcategory serviceprincipalname = $server.serviceprincipalname objectclass = $server.objectclass objectguid = $server.objectguid operatingsystemservicepack = $server.operatingsystemservicepack samaccountname = $server.samaccountname whenchanged = $server.whenchanged operatingsystem = $server.operatingsystem adspath = $server.adspath }) $Servers+=$CurrentServerObject}write-output $Servers
Thats a limitation of the searcher class.
There is an example here
http://msmvps.com/blogs/richardsiddaway/archive/2012/03/09/get-all-groups.aspx
that shows how to get to the data without the {}
Thanks for the link. Unfortunately I'm getting an error related to the "AD" drive ("Get-ChildItem : Cannot find drive. A drive with the name 'Ad' does not exist."). Is this drive related to some module I'm not loading, or a particular host OS?
I will continue to investigate this direction, however. If my issue has to do with the searcher object itself, looking for a different path to the same data may still provide the answer.
Thanks again,