且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Powershell 脚本来检查服务是否已启动,如果没有则启动它

更新时间:2023-12-04 20:18:10

我认为您的代码可能过于复杂:如果您只是检查服务是否正在运行,如果没有,请运行它然后停止重新评估,以下内容就足够了:

I think you may have over-complicated your code: 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:

刷新的好点.

$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'
    }

}