sheebs
sheebs

Reputation: 129

Best wait to ensure VM is back up after restart using PowerCLI?

I'm currently waiting for the guest OS to have an IP address, but it gets an IP address before Windows has completely rebooted (that is, before Windows has logged in and all startup services have run).

do
{
    Start-Sleep -Seconds 5
    Write-Host "Waiting for VM reboot"
    $VMInfo = Get-VM $VMName
    $GuestIP = $VMInfo.Guest.IPAddress
}
until ($GuestIP -eq $IP)

Anyone know of a better attribute to wait for?

Upvotes: 1

Views: 2905

Answers (1)

noam
noam

Reputation: 1983

You could use wait-tools instead. In practice, by the time VC knows the guest IP, vmtools (and most everything else) will already be up. So your method seems fine, assuming your guest has static IP. If your guest gets its IP from DHCP, there's always a chance the IP will change. In that case you could make sure $guestIP is not null, rather than comparing it against an expected value:

until ($GuestIP)

Needless to say, if there's some particular piece you need, the safest thing is to check that piece. Maybe gwmi win32_service or invoke-vmscript if the check warrants a bit of additional complexity.

Upvotes: 2

Related Questions