Use Powershell to check Status Windows Service and Start it

Ever fed up with services that you know of that they will stop eventually and need to be started manually, though you have set 'restart after 5 minutes' ? Me to! 

 

I'm using AlwaysUP for running Dropbox as a Service; works great!... until Dropbox Updater kicks in and kill's your process.
In AlwaysUP you can configure that it detects this behaviour and will restart it. 
The Application log says that it detected the stopping of the service, but it nevers get started again.

 

You can solve this by using Taskmanager and a Powershell script that checks the if the service is not running and starts it.

 

 

What to do

First check how your service is called in the Services list; look for its name.

 

Paste the following code in CheckService.ps1 and check the last line of code and adjust the servicename. 
Save it somewhere (i saved it in the root of C: drive)

 

function FuncCheckService{
param($ServiceName)
$arrService = Get-Service -Name $ServiceName
$Date = Get-Date -Format "MM/dd/yyyy HH:mm:ss"
$Start = "Started "
$Started = " service is already started."
if ($arrService.Status -ne "Running"){
Start-Service $ServiceName
($Date + " - " + $Start + $ServiceName) | Out-file D:\ServiceLogs\serviceStart.txt -append
}
if ($arrService.Status -eq "Running"){ 
($Date + " - " + $ServiceName + $Started) | Out-file D:\ServiceLogs\serviceStart.txt -append
}
}

FuncCheckService -ServiceName "Dropbox or other service name"

 

Then open taskmanager and add a basic task and give it a name
I chose to trigger it when a certain event is logged; in my case a event in the Application log with source AlwaysUP with eventID 1.
Set the delay for 10 minutes
 

Then under Action fill in Powershell.exe as program and in arguments the location and scriptname (in my case: C:\CheckService.ps1)
 

Make sure that the user that will run the service, has the right priveleges to do so.
Also check the security options, Run whether the user is logged on or not and check mark Run with highest privileges
Last but not least; under settings, change the dropdown box to 'stop the existing instance'