Friday, July 30, 2010

Powershell script to search Active Directory for servers

$strFilter = "(&(objectCategory=computer)(operatingSystem=Windows*Server*))"

$objDomain = New-Object System.DirectoryServices.DirectoryEntry("LDAP://OU=ou_name,dc=company,dc=com")

$objSearcher = New-Object System.DirectoryServices.DirectorySearcher

$objSearcher.SearchRoot = $objDomain

$objSearcher.PageSize = 1000

$objSearcher.Filter = $strFilter

$objSearcher.SearchScope = "Subtree"

$colProplist = "name"

foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}

$colResults = $objSearcher.FindAll()

foreach ($objResult in $colResults)

{$objItem = $objResult.Properties; $objItem.name

Write-Output $objItem.name | Out-File file.txt -append

}

1 comment:

Related Posts Plugin for WordPress, Blogger...