Is Computer Online

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

stakorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

footechCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
stakorAuthor Commented:
There appears to be one more "{" than "}"
0
footechCommented:
Sorry, missed that.  Put another "}" after line 16 to close the function.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.