powershell while循环不按预期工作

时间:2017-07-12 07:53:15

标签: powershell powershell-v4.0

我遇到了这个while循环的问题。即使状态以在线方式返回,它仍然停留在while循环中......任何想法都会令人惊讶:)

$instance = "(opsworks instance id)"

$status = (Get-OPSInstance -InstanceId $instance  -Region eu-west-1).Status.Trim()
Write-Host "Initial:$status"

while($status -ne 'online' -or $status -ne 'start_failed')
{
  $status = (Get-OPSInstance -InstanceId $instance  -Region eu-west-1).Status.Trim()
  Write-Host "Next:$status!"
}

1 个答案:

答案 0 :(得分:2)

This is a basic boolean algebra. It happens as online is not equal to start_failed. The -or operator will check both sides of the expression and will return true incase of either matches.

Condition

($status -ne 'online' -and $status -ne 'start_failed')

will be evaluated as false iff $status is neither online nor start_failed.