PowerShell snippet: “start a virtual machine and wait for a bit”
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
Comments
Anonymous
February 14, 2013
HI. I suggest adding also a timeout, for cases that the VM failes to start or something else fails, otherwise it might loop forever. YizharAnonymous
February 17, 2013
Although a failout might be good to, as a "bad" client would hang indefinitely.Anonymous
February 19, 2013
Good point. I will see if I can update this to include that.Anonymous
February 17, 2014
Hello, Another point of improvement would be support for virtual machines in Azure.