这是我的脚本,基本上可以刷新服务,一切正常,直到Start-Sleep 25
之后的最后一个状态检查为止。它一直拉取收到的第一个状态(我在该脚本中有几个检查状态,并且它使用第一个状态而不是重新检查它。)
例如,如果该服务处于脱机状态,它将使用首次检查时获取的第一个脱机状态,并在打开该服务后显示该状态。有什么方法可以让脚本重新检查状态并显示它,而不是使用第一个脚本?
$services = "RTCRGS"
$SvrName = 'CORPxxxx.xxxx.xxxx'
Get-Service -ComputerName $SvrName -Name $services | % {
Write-host "$($_.Name) on $SvrName is $($_.Status)"
if ($_.Status -eq 'stopped') {
Write-Host "Starting $($_.Name) in 5 sec..."
Start-Sleep 5
Write-Host "$($_.Name) is Starting, please wait..."
$_.Start()
Write-Host "Checking status of service, please wait for 25 sec..."
Start-Sleep 20
Write-Host "$($_.Name) on $SvrName is $($_.Status)"
} elseif ($_.Status -eq 'running') {
Write-Host "Restaring $($_.Name) in 5 sec..."
Start-Sleep 5
$_.Stop()
Start-Sleep 5
Write-Host "$($_.Name) is Starting, please wait..."
$_.Start()
Write-Host "Checking status of service, please wait for 25 sec..."
Start-Sleep 25
Write-Host "$($_.Name) on $SvrName is $($_.Status)"
}
}
答案 0 :(得分:0)
在start-sleep 20语句之后,执行新的get服务以刷新状态