我搜索了这个论坛和谷歌,但找不到我需要的东西.我有一个非常大的脚本,我正在寻找一些代码来检查服务是否已启动或停止,然后再继续下一步.
它自己需要循环的功能,直到它停止或启动(转到具有Stopped功能和启动功能).
总共4个服务几乎具有相同的名称,因此Service Bus*可以用作通配符.
mga*_*rde 37
我无法得到Micky发布的"计数"策略,所以我的解决方法如下:
我创建了一个函数,它接受searchString(这可能是"Service Bus*")以及我希望服务应该达到的状态.
function WaitUntilServices($searchString, $status)
{
# Get all services where DisplayName matches $searchString and loop through each of them.
foreach($service in (Get-Service -DisplayName $searchString))
{
# Wait for the service to reach the $status or a maximum of 30 seconds
$service.WaitForStatus($status, '00:00:30')
}
}
Run Code Online (Sandbox Code Playgroud)
现在可以调用该函数
WaitUntilServices "Service Bus *" "Stopped"
Run Code Online (Sandbox Code Playgroud)
要么
WaitUntilServices "Service Bus *" "Running"
Run Code Online (Sandbox Code Playgroud)
如果达到超时时间,则抛出一个不太优雅的异常:
Exception calling "WaitForStatus" with "2" argument(s): "Time out has expired and the operation has not been completed."
Run Code Online (Sandbox Code Playgroud)
Mic*_*lli 10
以下将循环并验证给定服务的状态,直到具有"正在运行"状态的服务数等于零(因此它们被停止),因此如果您正在等待服务停止,则可以使用此服务.
我添加了一个$MaxRepeat变量,它将阻止它永远运行.它将按照定义运行20次.
$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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
40073 次 |
| 最近记录: |