问题
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