I’ve compiled some scripts that I’ve had some help with to stop a service, make sure it’s stopped before starting the service back up, I think I have that part working. What I would like to add is if the service never comes back up after 3 try’s at 5 min a piece (300 sec)that it will give up and email out that the service restart has failed so we can manually intervene.
Any help would greatly be appreciated. I’m a novice at best.
$ServiceName = ‘Adobe Acrobat Update Service’
$arrService = Get-Service -Name $ServiceName
while ($arrService.Status -eq ‘Running’)
{
Stop-Service -Name $ServiceName
write-host $arrService.Status
write-host ‘Service Stopping’
start-sleep -seconds 10
$arrService.Refresh()
if ($arrService.Status -ne ‘Running’)
{
Write-Host ‘Service is now Stopped’
}
}
while ($arrService.Status -ne ‘Running’)
{
Start-Service $ServiceName
write-host $arrService.status
write-host 'Service starting'
Start-Sleep -seconds 10
$arrService.Refresh()
if ($arrService.Status -eq 'Running')
{
Write-Host 'Service is now Running'
}
It’d help immensely if you could format your code, as indicated in the bullet points above the posting text box. It’s difficult to follow the logic as-is.
$ServiceName = 'Foxit Reader Service'
$service = Get-Service -Name $ServiceName
$timeout = 0
while (($service.Status -ne 'Running') -and ($timeout -le 60)) {
$service.Refresh()
start-sleep -seconds 10
"{0} is {1}. Paused for {2} of 60 seconds." -f $ServiceName, $service.Status, $timeout
$timeout += 10
}
if ( $service.Status -eq 'Running' ) {
"{0} service is up and running and good to go" -f $ServiceName
}
else {
"{0} service didn't start in the time specified" -f $ServiceName
}