PowerShell script to find Windows computers in domain
When you are preparing for SQL Server inventory with any tool you will either have option to read entire AD for Server having SQL Server installed or you will need to feed the tool with a file having list of all the servers which you want to scan.
Following is the PowerShell script to list all the windows machines from you domain:
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 9000 # Mention here how many results you need
$objSearcher.Filter = '(OperatingSystem=*Window*)' # Filter for computer having words "Windows" in their operating system
# Following options also can be used
# '(OperatingSystem=*Window*Server*)'
# Above filter is for computer having words "Windows" and "Server" in their operating system
# '(OperatingSystem=*Window*Server*2008*)'
# Above filter is for computer having words "Windows" and "Server" and "2008" in their operating system
$colProplist = "name"
foreach ($i in $colPropList) {
$objSearcher.PropertiesToLoad.Add($i)
}
$colResults = $objSearcher.FindAll()
foreach ($objResult in $colResults) {
$objComputer = $objResult.Properties; $objComputer.name
}
Comments