Powershell-开始订购服务 [英] Powershell - Start ordered sequence of services

查看:259
本文介绍了Powershell-开始订购服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要启动一个有序的服务序列,我需要每个服务都将启动并运行,然后再尝试启动下一个服务,如何在Powershell中实现这一点?

I need to start an ordered sequence of services, i need that each service will be up and running before try to start the next one, how can I achieve this in Powershell? How can I wait for the stop too?

谢谢,

DD

推荐答案

如果您具有服务名称列表(例如数组),则foreach服务:

If you have a list of service names (say in an array), then foreach service:


  1. 获取其状态

  2. 如果未运行,则启动它

  3. 在循环中有延迟,请检查其状态直到它运行为止

密钥可能正在处理所有可能性对于第3项,包括服务失败。

The key is likely to be handling all the possibilities for #3 including the service failing.

但是轮廓大概是(不处理错误情况):

But an outline would be something like (without handling the error cases):

$serviceNames | Foreach-Object -Process {
  $svc = Get-Service -Name $_
  if ($svc.Status -ne 'Running') {
    $svc.Start()
    while ($svc.Status -ne 'Running') {
      Write-Output "Waiting for $($svc.Name) to start, current status: $($svc.Status)"
      Start-Sleep -seconds 5
    }
  }
  Write-Output "$($svc.Name) is running"
}

获取服务返回 System.ServiceProcess.ServiceController ,它是实时的,指示服务的当前状态,而不仅仅是创建实例时的状态。

Get-Service returns an instance of System.ServiceProcess.ServiceController which is "live"—indicating the current state of the service, not just the state when the instance was created.

类似的停止过程将用已停止和启动替换正在运行 ,然后点击停止。而且,大概可以颠倒服务列表的顺序。

A similar stop process would replace "Running" with "Stopped" and the "Start" call with "Stop". And, presumably, reverse the order of the list of services.

这篇关于Powershell-开始订购服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆