• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 63
  • Last Modified:

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

0
stakor
Asked:
stakor
  • 2
1 Solution
 
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
 
stakorAuthor Commented:
There appears to be one more "{" than "}"
0
 
footechCommented:
Sorry, missed that.  Put another "}" after line 16 to close the function.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now