I know there are several similar posts on the subject, but what I would like to know is how to re-start a service that has already been stopped. Is there a .bat or .vbs script that would check if the service is stop and then restart it if it's stopped and run in a loop?

Thank you for your help!

Update 7.IX.2012 @ 11.25 I understand there are several posts that go into details on usage of SC and net stop & net start, most of those posts are dealing with stopping the service first and then restarting it. I am dealing with the service that already either crashed or stopped and I need to check if it is stopped and then restart it. I hope I am making it clearer. It would also be nice to have a log file attached to it too.


As an example, create a batch file C:\derp.bat

Contents of batch file could look something like this:

net start "Service Name"

C:\derp.bat

First line: If the service is already running then nothing will happen. If it's not running then it will attempt to start.

Second line: restarts the batch file.

You could throw a ping 127.0.0.1 -n 60 in the middle if you wanted it to try every 60 seconds.


Starting a service in PowerShell is as easy as (using spooler as an example):

Start-Service spooler

You can also check if a service is running and have it restarted if it's not via:

$Service = Get-Service -Name spooler
if ($Service.Status -ne "Running")
{         
    (Get-Date).ToString() + " - Service stopped." >> C:\Scripts\log.txt
    Start-Service spooler
}

However, I wouldn't recommend looping a script infinitely. A quick and dirty solution would be to set this script to run every so often with a Scheduled Task.

Edit: Added a line to add an entry to a log file.