We help IT Professionals succeed at work.

Is Computer Online

stakor
stakor asked
on
I am trying to scan an OU's worth of machines. There are a number that are not online. Because of this, when I run the small script below, it produces a lot of errors.

Is there a way of checking to see if the machine in question is online before checking to see if the service in question is running or not. At the moment, the script generates a lot of false positives, and a lot of errors.

function FuncCheckService{
param($ServiceName)
Import-Module ActiveDirectory
$Servers = Get-ADComputer -Filter * -SearchBase "OU=Deeper,OU=Deep,Dc=Domain,dc=Companies,dc=com" -SearchScope 2 | Select Name
ForEach ($Server in $Servers) {
	$arrService = Get-Service -ComputerName $Server -Name $ServiceName
	if ($arrService.Status -ne "Running"){
		Write-Host "$ServiceName on $Server is NOT Running" -ForegroundColor Red
		}	
	ELSE{ 
		Write-Host "$ServiceName service on $Server is Running" - ForeGroundColor Green
		}
	}
}
FuncCheckService -ServiceName "SERVICE_NAME"

Open in new window

Comment
Watch Question

Top Expert 2014
Commented:
This is pretty much what Test-Connection is for.
function FuncCheckService{
param($ServiceName)
Import-Module ActiveDirectory
$Servers = Get-ADComputer -Filter * -SearchBase "OU=Deeper,OU=Deep,Dc=Domain,dc=Companies,dc=com" -SearchScope 2 | Select Name
ForEach ($Server in $Servers) {
    If ( Test-Connection $Server -Count 1 -Quiet )
    {
        $arrService = Get-Service -ComputerName $Server -Name $ServiceName
        if ($arrService.Status -ne "Running"){
            Write-Host "$ServiceName on $Server is NOT Running" -ForegroundColor Red
        }	
        ELSE{ 
            Write-Host "$ServiceName service on $Server is Running" - ForeGroundColor Green
        }
    }
}
FuncCheckService -ServiceName "SERVICE_NAME"

Open in new window

Author

Commented:
There appears to be one more "{" than "}"
Top Expert 2014

Commented:
Sorry, missed that.  Put another "}" after line 16 to close the function.