My status won't update and it keep pulling from the previous status

…衆ロ難τιáo~ 提交于 2019-12-11 17:30:01

问题


Here my script, basically it refreshes service, everything is working fine until that last status check after Start-Sleep 25. It keeps pulling the first status that it received (I have couple check status in this script and it uses the first status instead of rechecking it).

For example, if the service is offline and it will use the first offline status that was acquired when checking for the first time and display it after the service have been turned on. Any way to show have the script recheck the status and display it instead of using the first one?

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

回答1:


After the start-sleep 20 statement do a new get-service to refresh status



来源:https://stackoverflow.com/questions/51788419/my-status-wont-update-and-it-keep-pulling-from-the-previous-status

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!