Start – Stop Service with Powershell and Ping

Ran into a request for an OCS Mediation Server that is contacting an OCS Front End Pool over a WAN link.

“We have a remote OCS Mediation server that contacts the OCS Front End Pool over a WAN link. I need that Mediation server to ping those front end servers periodically to verify that the WAN link is up. If the ping on two of the servers fails,  I need it to stop the OCS Mediation service on the mediation server until it can successfully ping the two front end server across the WAN again at which point it would then bring the Service on the Mediation server back up.”

 

With a LOT of assitance from Jeffery Hicks and Claus Thude Nielsen a simple script was created.

I interpreted the need to stop the service if both computers are unreachable and start the service when they are. Plug in your values. $server1=”coredc01″
$server2=”client1″
$service=”spooler”if ((test-connection $server1 -quiet) -AND (test-connection $server2 -quiet)) {
#both servers are reeachable so make sure service is running
get-service $service | where {$_.status -eq “Stopped”} | Start-Service -PassThru
}
else {
#at least one server is unreachable so stop service
write-warning “One of the servers is unreachable”
get-service $service | where {$_.status -eq “Running”} | Stop-Service -PassThru
}

Jeffery Hicks
Windows PowerShell MVPNow Available: Windows PowerShell 2.0: TFM

  In this example the Two Server that are being PINGED are “coredc01” and “client1”.  The service being stopped and or started is “spooler”.

A scheduled task – set to run as a System Process – can be used to run the PS1 script.

Just for future reference… I simply placed the PS1 file in a temp folder on C:\ for testing. Then I created a scheduled task set to run every minute. In the RUN box (property of the scheduled task) where it was listed –

 

 

C:\temp\restart.ps1 

I added this bit – 

powershell.exe -noexit C:\temp\restart.ps1

It was automatically changed to this –

C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -noexit C:\temp\restart.ps1

-noexit keeps the powershell window open so I could see any warnings or MSG. NOTE! If running via scheduled task, the task will have a status of running and will not open a new window until the first one closes.

Also,,, if you are simply removing the network cable to signify a down connection, make sure you have turned off your wireless connection as well. May save you about 20 Min’s of head scratching. <g>

Once I was satisfied with the PS1 working I then removed the -noexit. Works like a charm…
Another example where the PS1 is set to run contniuous –

$server1=”cropfrontend1″
$server2=”corpfrontend2″
$service=”OCS Communication Server Mediation”
do{
if ((test-connection $server1 -quiet) -OR (test-connection $server2 -quiet)) {
#if 1 server is reachable make sure service is running
get-service $service | where {$_.status -eq “Stopped”} | Start-Service -PassThru
}
elseif (!(test-connection $server1 -quiet) -AND (test-connection $server2 -quiet)) {
#if both servers are unreachable stop service
write-warning “Both of the servers are unreachable”
get-service $service | where {$_.status -eq “Running”} | Stop-Service -PassThru
}
start-sleep –s 15
} until ( $true –eq $false)

One Response to Start – Stop Service with Powershell and Ping

  1. tribute von panem…

    […]Start – Stop Service with Powershell and Ping « Tim Bolton – MCITP – MCTS[…]…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: