April 12

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


Copyright 2021. All rights reserved.

Posted April 12, 2017 by Timothy Conrad in category "Programming

About the Author

If I were to describe myself with one word it would be, creative. I am interested in almost everything which keeps me rather busy. Here you will find some of my technical musings. PGP: 4CB8 91EB 0C0A A530 3BE9 6D76 B076 96F1 6135 0A1B