PowerShell snippet: “start a virtual machine and wait for a bit”

PowerShell snippet: “start a virtual machine and wait for a bit”

Rate This
  • Comments 4

Over the last year there are a couple of PowerShell functions that have become a common set of many of my scripts.  One example is this “StartVMAndWait” function:

function StartVMAndWait($vmToStart){
start-vm $vmToStart
do {Start-Sleep -milliseconds 100} 
    until ((Get-VMIntegrationService $vmToStart | ?{$_.name -eq "Heartbeat"}).PrimaryStatusDescription -eq "OK")
}

This is a simple function that starts a virtual machine and then waits until the heartbeat integration component has returned a healthy status.  This is a good indicator that the guest operating system has completed booting – which makes this quite handy when you want to start a virtual machine and then interact with the guest operating system in some way.

Cheers,
Ben

Leave a Comment
  • Please add 3 and 4 and type the answer here:
  • Post
  • HI.

    I suggest adding also a timeout, for cases that the VM failes to start or something else fails,

    otherwise it might loop forever.

    Yizhar

  • Although a failout might be good to, as a "bad" client would hang indefinitely.

  • Good point.  I will see if I can update this to include that.

  • Hello,

    Another point of improvement would be support for virtual machines in Azure.

Page 1 of 1 (4 items)