简体   繁体   English

Powershell,检查服务是否正在运行,如果没有,请启动它

[英]Powershell, check that the service is running and if it is not, start it

I have created a powershell script that detects if X service is active or not, if not, it starts it.我创建了一个 powershell 脚本来检测 X 服务是否处于活动状态,如果没有,它会启动它。 I also wanted to add a parameter in the while to avoid that the script gets stuck and after several attempts (2) it closes the script.我还想在 while 中添加一个参数,以避免脚本卡住,并在多次尝试后 (2) 关闭脚本。

The script works, it starts the service, but it tries to do the or even though the first statement was true.该脚本有效,它启动服务,但它尝试执行 or 即使第一条语句为真。 And not the reason why it does not work:而不是它不起作用的原因:

$ServiceName = 'MSIREGISTER_MR'
$arrService = Get-Service -Name $ServiceName
$conteo = 0

 

while (($arrService.Status -ne 'Running') -or ($conteo -ne '2'))
{

 

    Start-Service $ServiceName
    write-host $arrService.status
    write-host 'Arrancando el servicio...'
    $conteo = $conteo +1
    Start-Sleep -seconds 5
    $arrService.Refresh()
    if ($arrService.Status -eq 'Running')
    {
        Write-Host 'El servicio esta arrancado'
    }
    else 
    {
        Write-Host 'Fallo al arrancar el servicio'
    }

 

}

Why this is happening为什么会发生这种情况

In your script today, you check for the status of the service only once, at the top of the script:在您今天的脚本中,您只在脚本顶部检查服务状态一次:

$arrService = Get-Service -Name $ServiceName

As you can see here, if I try the same using the Xbox Live Game Save Service which is currently stopped on my PC...正如您在此处看到的,如果我尝试使用当前在我的 PC 上停止的 Xbox Live 游戏保存服务...

$service = get-service XblGameSave

Status   Name               DisplayName
------   ----               -----------
Stopped  XblGameSave        Xbox Live Game Save

C:\git\core> $service.Start()
C:\git\core> $service

Status   Name               DisplayName
------   ----               -----------
Stopped  XblGameSave        Xbox Live Game Save

I made a variable, I checked it, then I started the service.我做了一个变量,我检查了它,然后我启动了服务。

When I check, the Xbox Live Game save service, is actually started , but my variable doesn't know about it.当我检查时,Xbox Live Game 保存服务实际上已启动,但我的变量不知道。

However, when I refresh the variable, I can now see the new status.但是,当我刷新变量时,我现在可以看到新状态。

PS> $service.Refresh()

Status   Name               DisplayName
------   ----               -----------
Running  XblGameSave        Xbox Live Game Save

Refreshing the variable with .Refresh() is a VERY weird thing that basically nothing else in PowerShell does other than services.使用.Refresh()刷新变量是一件非常奇怪的事情,PowerShell 中除了服务之外基本上没有其他功能。

What you need to do你需要做什么

You need to update your code to check within the loop every time, to see if the service is now running or not.您需要更新代码以每次都在循环内进行检查,以查看服务现在是否正在运行。

Also, your condition on the loop seems incorrect.此外,您在循环中的条件似乎不正确。

while (($arrService.Status -ne 'Running') -or ($conteo -ne '2'))

This should run until the service is either running, or $conteo is greater than 2 (from your description).这应该一直运行,直到服务正在运行,或者$conteo大于 2(根据您的描述)。 Here's how that should to do that in code.这是在代码中应该如何做到这一点。

while (($arrService.Status -ne 'Running') -or ($conteo -gt '2'))

There's a logic flaw in your conditional ;您的条件中存在逻辑缺陷 change it to:将其更改为:

while ($arrService.Status -ne 'Running' -and $conteo -lt 2) { ... 

That way, you exit the loop either once the service was started successfully or if $conteo reaches value 2 , ie after the 2nd unsuccessful start attempt.这样,一旦服务成功启动$conteo达到值2 ,即在第二次启动尝试失败后,您就退出循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM