Question about Scripting Guys script
i checking out script scripting guy gather information active directory gather list attributes have run issue.
here script:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$strcategory = "computer" $objdomain = new-object system.directoryservices.directoryentry $objsearcher = new-object system.directoryservices.directorysearcher $objsearcher.searchroot = $objdomain $objsearcher.filter = ("(objectcategory=$strcategory)") $colproplist = "name" foreach ($i in $colproplist){$objsearcher.propertiestoload.add($i)} $colresults = $objsearcher.findall() foreach ($objresult in $colresults) {$objcomputer = $objresult.properties; $objcomputer.name
the way explained understand add attributes array $colproplist = "name" such $colproplist = "name", "lastlogintimestamp"
and that would return value needed; however, still returns name. can provide insight on how modify script to
gather additional ad attributes?
"give me army of west point graduates, i'll win battle. give me handful of texas aggies , i'll win war!" --gen. george s. patton, jr.
$colproplist = "name", "operatingsystem", "lastlogontimestamp"
foreach ($i in $colproplist){$objsearcher.propertiestoload.add($i)}
this tells properties load.
the best thing powershell not can create scripts can execute things @ in shell can see outcome of each command...
so if run code point of $colresults = $objsearcher.findall()... return computers match filter supplied...
if have more 1 can access them index (array have indexs, 0,1,2,3 etc)
so if type in $colresults[0] first directoryentry object in list of type computer...
if run
$colresults[0] | get-member
it shows of members (members properties, methods, events etc) object (in case object directoryentry) has...
the 1 care properties, again, inspect it...
$colresults[0].properties | get-member
two things of interest there, propertynames , values... so... object doing bit of translation us...
$colresults[0].properties.propertynames
it spits out properties loaded... you'll notice lower case...
so if try access properties loaded, loaded them, example...
$colresults[0].properties.operatingsystem
nothing?
why?
well, 1 of few cases case matters...
try $colresults[0].properties.operatingsystem
there is!
the property used lower case name, why got name...
{$objcomputer = $objresult.properties; $objcomputer.name; $objcomputer.operatingsystem; $objcomputer.lastlogontimestamp}
so, code work fine if make property names lower case...
{$objcomputer = $objresult.properties; $objcomputer.name; $objcomputer.operatingsystem; $objcomputer.lastlogontimestamp}
justin
Windows Server > Windows PowerShell
Comments
Post a Comment