简体   繁体   English

Powershell - 启动有序的服务序列

[英]Powershell - Start ordered sequence of services

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?我需要启动一个有序的服务序列,我需要在尝试启动下一个服务之前启动并运行每个服务,我怎样才能在 Powershell 中实现这一点? How can I wait for the stop too?我怎么能等到停止呢?

Thanks,谢谢,

DD DD

Don't do this manually (regardless of scripting language). 请勿手动执行此操作(无论脚本语言如何)。 Define proper dependencies between the services and Windows will start/stop them in the correct order. 在服务之间定义适当的依赖关系,Windows将以正确的顺序启动/停止它们。 You can use the sc utility to define the dependencies: 您可以使用sc实用程序定义依赖项:

sc config Svc2 depend= Svc1

If a service should depend on more than one other service you separate the dependent services with forward slashes: 如果一项服务应依赖于一个以上的其他服务,则可以使用正斜杠来分隔从属服务:

sc config Svc5 depend= Svc3/Svc4

Note that the = must be followed by a space and must not be preceded by one. 请注意, =后面必须有一个空格,且不得在前跟一个空格。

If you have a list of service names (say in an array), then foreach service: 如果您有服务名称列表(例如数组),则foreach服务:

  1. Get its status 获取其状态
  2. If not running, then start it 如果没有运行,则启动它
  3. With a delay in the loop, check its status until it is running 延迟循环,检查其状态,直到运行

The key is likely to be handling all the possibilities for #3 including the service failing. 关键可能是处理#3的所有可能性 ,包括服务失败。

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"
}

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. Get-Service返回System.ServiceProcess.ServiceController的实例,该实例处于“活动”状态-指示服务的当前状态,而不仅是创建该实例时的状态。

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. 并且,大概颠倒了服务列表的顺序。

To stop Services 停止服务

$ServiceNames = Get-Service | where {($_.Name -like "YourServiceNameHere*")-and ($_.Status -eq "Running")}
 Foreach-Object {$_.(Stop-Service $serviceNames)}

To start Services 启动服务

$ServiceNames = Get-Service | where {($_.Name -like "YourServiceNameHere*")-and ($_.Status -ne "Running")}
Foreach-Object {$_.(Start-Service $ServiceNames)}

To start and wait in order启动和等待顺序

[array]$Services = 'svc1', 'svc2', 'svc3'

$Services | foreach {
    start-service $_
    (get-service $_).WaitForStatus('Running')
}

The wait for status might not be needed.可能不需要等待状态。 I have no system that have slow services to test with.我没有可以测试服务速度慢的系统。

To stop and wait in order停下来按顺序等待

[array]$Services = 'svc1', 'svc2', 'svc3'
[array]::Reverse($Services)

$Services | stop-service

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

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