PS1 script listing computers in a domain


i working on script run through domain , spit out report of computers in domain. put following togehter , works great in test domain (which has 8 computers in it. when run in of prod domains (thousands of computers) gives odd results found 4 computers. code , results below:

--------------------start script----------------------------------------

$domain = 'ldap://localhost:389/dc=xx,dc=xxx,dc=xxx'

$root = new-object directoryservices.directoryentry $domain

$date = date

## debugging uncomment 3 lines below , verify ldap connection domain

##cls

##write-host "`n`nconnected ldap domain:`n $domain `n"

##"the domain name is: $($root.name) `n`n"

"the following computers in $($root.name) domain" >> ./adcomputerinformation.txt

"report date: $date" >> ./adcomputerinformation.txt

" " >> ./adcomputerinformation.txt

$selector = new-object directoryservices.directorysearcher

$selector.searchroot = $root

$selector.searchscope = "subtree"

$adobj = $selector.findall() `

| {$_.properties.objectcategory -like "cn=computer*"}

foreach ($computer in $adobj){

$prop = $computer.properties

" $($prop.distinguishedname) " >> ./adcomputerinformation.txt

}

" " >> ./adcomputerinformation.txt

"there $($adobj.count) computers in $($root.name) domain" >> ./adcomputerinformation.txt
---------------------------------end script-------------------------


results domain thousands of pcs {slightly scrubbed protect innocent}:

--------------------start report-----------------------------------------------------
the following computers in the xx domain
report date: 03/26/2009 14:02:37
 
  cn=dc1old,ou=servers,ou=location servers,dc=xx,dc=xxx,dc=xxx 
  cn=dc1,ou=domain controllers,dc=xx,dc=xxx,dc=xxx 
  cn=dc2,ou=domain controllers,dc=xx,dc=xxx,dc=xxx
  cn=corpfileserver,ou=file servers,ou=location servers,dc=xx,dc=xxx,dc=xxx 
 
there 4 computers in the xx domain

--------------------------end report-------------------------------------

like said thing accurate in test domain innacurate in several of prod domains. sure there better way write output report file best figure @ point. thoughts or suggestions welcome.

thanks,

-russ

hi,

your filter not correct. moreover, prefere use builtin filter option pipe chain cmdlet.

all code resumed 5 lines:

$selector = new-object directoryservices.directorysearcher
$selector.filter = '(objectclass=computer)'
$selector.pagesize = 10000 # because object size limited 1000 entries default
$selector.propertiestoload.add('distinguishedname') # required property : limit network trafic
$selector.findall() | % { $_.properties.distinguishedname | out-file adcomputerinformation.txt -append }

hope helps
grégory schiro - powershell & mof - http://scriptingof.blogspot.com


Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

Azure MFA with Azure AD and RDS

Failed to setup initiator portal. Error status is given in the dump data.

Invalid pointer on gpresult /h gpreport.html