Pages

3/27/2011

Powershell: List AD Machine Accounts


######################################################################
#
# List all Windows Servers with machine accounts in Active Directory
#
######################################################################

$ServerList = 'c:\allservers.txt'
New-Item $serverlist -Type file -Force >$nul

$strCategory = "computer" 
$strOS = "Windows*Server*"
$objDomain = New-Object System.DirectoryServices.DirectoryEntry 
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher 
$objSearcher.SearchRoot = $objDomain
$objSearcher.Filter = ("OperatingSystem=$strOS")
$colProplist = "dnshostname"
foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}
$colResults = $objSearcher.FindAll()

foreach ($objResult in $colResults)  { 
  $objComputer = $objResult.Properties;  
  $Server = $objComputer.dnshostname
  $Server = $Server -replace "\s{2,}", ""
  $Server = $Server -replace "\.US\.DOMAIN\.COM", ""
  write-output $Server | out-file -encoding ASCII -filepath $ServerList -append
  }#foreach

No comments: