Friday, 18 October 2024

Powershell script to check if service is started, if not then start it

 

Powershell script to check if service is started, if not then start it

 

If you are just checking to see if a service is running and, if not, run it and then stop re-evaluating, the following should suffice:

Good point on the refresh.

$ServiceName = 'Serenade'
$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'
    }

}

 

No comments:

Post a Comment

Note: only a member of this blog may post a comment.

Blog Archive