我的状态不会更新,并保持它从以前的状态拉(My status won't update an

2019-10-29 07:42发布

在这里我的脚本,基本上它刷新服务,一切工作正常,直到之后的最后状态检查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)"
    }
}

Answer 1:

启动睡眠20语句后做一个新的获取服务刷新状态



文章来源: My status won't update and it keep pulling from the previous status