Hi,
I am trying to wait for a VM to reboot, and the guest OS to be running before it disables the NIC. However, the script doesn't wait for the VM to even power back on before it disables the NICs. I thought about putting a sleep command right after the restart-vmguest but some servers take longer than others to restart.
Any ideas on how I can improve this script so it doesn't disable the NIC until after the server has rebooted, and the guest OS is back up?
write-host "Restarting server" -ForegroundColor Yellow
Restart-VMGuest $server
while ($server.PowerState -eq 'PoweredOff'){
sleep 5
}
# disable all NICs
write-host "Disabling NICs" -foregroundcolor Yellow
$nic = Get-VM $server | Get-NetworkAdapter
ForEach ($net in $nic) {
Set-NetworkAdapter -NetworkAdapter $net -Connected:$false -startconnected:$false -Confirm:$false
}
You can use the Get-VMGuest | Select State to waiting for state -eq Running
I.e.
#Wait for Power OFF
while ($server.PowerState -eq 'PoweredON'){
sleep 5
}
#Wait for VM Guest OS Running
while ((Get-VMGuest $Server).State -NE "Running"){
sleep 5
}
Hi,
Thanks. I tried that and it's still not waiting for the guest to restart. Less than 3 seconds after I see "restarting server" in yellow text, I see the next line in yellow saying "Disabling NICs" Below is my updated code, thoughts?
write-host "Restarting server" -ForegroundColor Yellow
Restart-VMGuest $server
while (($server.PowerState -eq 'PoweredOff') -AND ((Get-VMGuest $server).State -NE "Running")){
sleep 5
}
# disable all NICs
write-host "Disabling NICs" -foregroundcolor Yellow
$nic = Get-VM $server | Get-NetworkAdapter
ForEach ($net in $nic) {
Set-NetworkAdapter -NetworkAdapter $net -Connected:$false -startconnected:$false -Confirm:$false
}
I updated my response, as I jumped the gun a bit.
Update2: Wait, does Restart-VMGuest ever produce $Server.PowerState -eq "OFF"
I haven't tested but I'll bet it doesn't because the virtual hardware doesn't need to power off for a reboot
Might need to just test on
#Wait for PowerOFF of the Guest OS
while ((Get-VMGuest $server).State -EQ "Running"){
sleep 5
}
#Wait for PowerON of the Guest OS
while ((Get-VMGuest $server).State -NE "Running"){
sleep 5
}
Since you are restarting the Guest OS, the VM itself will never be powered off.
A better way is to wait till the VMware Tools are actually stopped.
And then wait for them to be running again
$vmGuest = Restart-VMGuest -VM $server
# Wait till VMware Tools stop running
while($vmGuest.State -eq 'Running'){
sleep 2
$vmGuest = Get-VMGuest -VM $vmGuest.VM
}
# Then wait till the VMware Tools are running again
while ($vmGuest.State -ne 'Running') {
Start-Sleep 2
$vmGuest = Get-VMGuest -VM $vmGuest.VM
}
# disable all NICs
Write-Host "Disabling NICs" -foregroundcolor Yellow
$nic = Get-VM -Name $server | Get-NetworkAdapter
ForEach ($net in $nic) {
Set-NetworkAdapter -NetworkAdapter $net -Connected:$false -startconnected:$false -Confirm:$false
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks LucD,
It's pretty close except now it just sits at the 'Restarting Server' text on the script window, even after the guest OS has booted and VM tools show as running in vCenter. Any ideas on why this may be? I have pasted my updated code below:
write-host "Restarting server" -ForegroundColor Yellow
#Restart-VMGuest $server
$vmGuest = Restart-VMGuest -VM $server
# Wait till VMware Tools stop running
while($vmGuest.State -eq 'Running'){
sleep 2
$vmGuest = Get-VMGuest -VM $vmGuest.VM
}
# Then wait till the VMware Tools are running again
while ($vmGuest.State -ne 'Running') {
Start-Sleep 2
$vmGuest = Get-VMGuest -VM $vmGuest.VM
}
# disable all NICs
write-host "Disabling NICs" -foregroundcolor Yellow
$nic = Get-VM $server | Get-NetworkAdapter
ForEach ($net in $nic) {
Set-NetworkAdapter -NetworkAdapter $net -Connected:$false -startconnected:$false -Confirm:$false
}
Yes, I just did some further testing with different types of guest OS.
The basic problem is that all the properties under Guest are updated, on a schedule, by the VMware Tools.
While the guest OS is restarting the VMware Tools are consequently stopped and started.
But that means that between that stop-start there will be no updates of the properties under Guest.
Hence you can't use any of those to do the testing.
The only way I could make a wait valid for all types of guest OS was by interacting with the guest OS.
For example a ping.
That interaction should briefly go away during the guest OS reboot.
To avoid this, the best way to organise what you want to do is to use a Shutdown-VMGuest.
That way you wait for the PowerState to change to 'poweredoff'.
Do your vNIC actions and then start the VM.
Shutdown-VMGuest -VM $server
Write-Host "Waiting for guest to power off" -ForegroundColor Yellow
while((Get-VM -Name $server).PowerState -ne 'poweredoff'){
sleep 2
}
# disable all NICs
Write-Host "Disabling NICs" -foregroundcolor Yellow
$nic = Get-VM -Name $server | Get-NetworkAdapter
ForEach ($net in $nic) {
Set-NetworkAdapter -NetworkAdapter $net -Connected:$false -startconnected:$false -Confirm:$false
}
Write-Host "Start the VM" -foregroundcolor Yellow
Start-VM -VM $server
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference