title: Powershell/PowerCLI Loop, timeouts and exits
body: <p>Here is the scenario - I'm remotely starting a VM via Powershell/PowerCLI (VMwares Powershell module) and once the VM is started I will be running a series of cmdlets against the VM.</p>
  
  <p>Currently I have this bit of code:</p>
  
  <pre><code>Start-VM "my VM Name" -runAsync
  $vm = Get-VM | where { $_.name -eq "my VM Name" }    
  
  start-sleep -seconds 20
  do {
      start-sleep -seconds 5
      $toolsStatus = ($VM | Get-View).Guest.ToolsStatus
  } until ($toolsStatus -eq 'toolsOK')
  </code></pre>
  
  <p>Which works - the VM starts and the loop will check until VMware Tools is reporting in to VMware (which means the VM has fully booted into the OS).</p>
  
  <p>However this will run indefinitely and so in a scenario where the VM fails to boot successfully - the script hangs. So I've tried to add in a timer to exit the script if it runs passed by 5 Minutes:</p>
  
  <pre><code>Start-VM "My VM Name" -runAsync
  $VM = Get-VM | where { $_.name -eq "My VM Name" }
  
  $timeout = new-timespan -minutes 5
  start-sleep -seconds 5
  $toolsStatus = ($VM | Get-View).Guest.ToolsStatus
  $sw = [diagnostics.stopwatch]::StartNew()
  while ($sw.elapsed -lt $timeout) {
      if ($toolsStatus -eq 'toolsOK') {
          return
      }
      start-sleep -seconds 5
  }
  Exit
  </code></pre>
  
  <p>Which successfully exits the script after 5 minutes. The issue is whilst I'm testing this - the VM has booted and I'm getting a response indicating that VMtools has responded, but the loop isn't stopping. It's continuing till it has timed-out then exiting as opposed to proceeding to the next step.</p>
  
  <p>I've not worked with this type of loop before so would appreciate some input as I'm sure I'm close - but I've missed something.</p>