Powershell 等待服务停止或启动 [英] Powershell Wait for service to be stopped or started

查看:95
本文介绍了Powershell 等待服务停止或启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这个论坛和谷歌上搜索过,但找不到我需要的东西.我有一个相当大的脚本,我正在寻找一些代码来检查服务是启动还是停止,然后再继续下一步.

I have searched both this forum and through google and can't find what I need. I have a quite large script and I'm looking for some code that will check if the service is started or stopped before proceeding to the next step.

函数本身需要循环,直到它停止或启动(将有一个函数用于停止,一个用于启动).

The function it self need to loop untill it's either stopped or started (Going to have a function for Stopped and one for Started).

总共有 4 个服务几乎同名,所以 Service Bus * 可以用作通配符.

In total 4 services which almost have the same name, so Service Bus * can be used as a wildcard.

推荐答案

以下将循环并验证给定服务的状态,直到具有正在运行"状态的服务数量为零(因此它们被停止),因此您可以在等待服务停止时使用它.

The following will loop and verify the status of the given services until the number of services with the "Running" state is equal to zero (hence they are stopped), so you can use this if you are waiting for services to Stop.

我添加了一个 $MaxRepeat 变量,这将永远阻止它运行.它将按照定义最多运行 20 次.

I've added a $MaxRepeat variable, which will prevent this from running for ever. It will run 20 times max as defined.

$services = "Service Bus *"
$maxRepeat = 20
$status = "Running" # change to Stopped if you want to wait for services to start

do 
{
    $count = (Get-Service $services | ? {$_.status -eq $status}).count
    $maxRepeat--
    sleep -Milliseconds 600
} until ($count -eq 0 -or $maxRepeat -eq 0)

这篇关于Powershell 等待服务停止或启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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