5

I want to run a script to check if 5 servers are up and running based on a specific service is running.If that service is running then we know that server is up and accesible. If it does not reply with a response back then I want it to continously check for it. Heres what I got so far:

Get-Service LANMANSERVER -ComputerName JOHNJ1
Get-Service LANMANSERVER -ComputerName JOHNJ2
Get-Service LANMANSERVER -ComputerName JOHND1
Get-Service LANMANSERVER -ComputerName JOHNM
Get-Service LANMANSERVER -ComputerName JOHNI
start-sleep 90

1 Answer 1

8

Yep you just need to check the Status property on the returned object like this:

$servers = "JOHNJ1", "JOHNJ2"
foreach ($server in $servers) {
    $status = (get-service -Name lanmanserver -ComputerName $server).Status
    if ($status -eq "Running") {
        "Its Up!"
    } else {
        "Its Down!"
    }
}

Update Here is an example of how to wait for a server to become online:

$servers = "JOHNJ1", "JOHNJ2"
foreach ($server in $servers) {
    while ( (get-service -Name lanmanserver -ComputerName $server).Status -ne "Running" ) {
        "Waiting for $server ..."
        Start-Sleep -Seconds 10
    }
    "$server is Up!"
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks would I need a while loop to continously check until that server was up based on it checkign for that service?
@BigCAT41 Yep, I added an example of how to do that.
$servers = "JOHNJ01", "JOHNJ02", "JOHNI01", "JOHN01", "JOHND01"; foreach ($server in $servers) { $status = (get-service -Name spooler -ComputerName $server).Status $server if ($status -eq "Running") { "Its Up!" } else { "Its Down!" foreach ($server in $servers) { while ( (get-service -Name spooler -ComputerName $server).Status -ne "Running" ) { "Waiting for $server ..." Start-Sleep -Seconds 10 } "$server is Up!" } } } start-sleep 90
@BigCAT41 Glad it helped. If you found this answered your question please mark it as the accepted answer. Thank you.
Is there any way to avoid the red error messages: get-service : Cannot find any service with service name 'lanmanserver'. At D:\scripts\ServerMonitor.ps1:3 char:16 + $status = (get-service -Name lanmanserver -ComputerName $server).Status + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (lanmanserver:String) [Get-Service], ServiceCommandException + FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetServiceCommand
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.