16 July 2007

PowerShell: getting all Active Directory computers running a particular service

$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)
{
$temp=[System.ServiceProcess.ServiceController]::GetServices($objComputer.name)
where{$_.name -eq 'Dhcp'}
If($temp.status -eq "Running")
{
Write-host $objComputer.name
}
}

No comments: