Go to ...

Techpository

A Better Technical Repository

RSS Feed

Programming: A powershell way to check to see if a service has stopped and then start it.


To check to see if a service is running and then start it if it is not, create a powershell file with the following code.

$ServiceName = ‘YourExactServiceName’
$arrService = Get-Service -Name $ServiceName
while ($arrService.Status -ne ‘Running’)
{
Start-Service $ServiceName
write-host $arrService.status
write-host ‘Service starting’
Start-Sleep -seconds 60
$arrService.Refresh()
if ($arrService.Status -eq ‘Running’)
{
Write-Host ‘Service is now Running’
}
}

You can set this up in the Windows  task scheduler with the command:

powershell -file “c:\YourDirectory\YourPowershellFile.ps1”

By: D. Gugg, N. Eagle, T Conrad