How do I shutdown the Host over ssh on ESXi 5 so it shuts down the guests properly?
14,897
I think I found an answer. This script could do it:
http://www.virtu-al.net/2010/01/06/powercli-shutdown-your-virtual-infrastructure/
Please note the part in line 17ff where it waits for the VMs to be Shutdown cleanly
Thank you Sergei!
Connect-VIServer MyVIServer 2 3 # Get All the ESX Hosts 4 $ESXSRV = Get-VMHost 5 6 # For each of the VMs on the ESX hosts 7 Foreach ($VM in ($ESXSRV | Get-VM)){ 8 # Shutdown the guest cleanly 9 $VM | Shutdown-VMGuest -Confirm:$false 10 } 11 12 # Set the amount of time to wait before assuming the remaining powered on guests are stuck 13 $waittime = 200 #Seconds 14 15 $Time = (Get-Date).TimeofDay 16 do { 17 # Wait for the VMs to be Shutdown cleanly 18 sleep 1.0 19 $timeleft = $waittime - ($Newtime.seconds) 20 $numvms = ($ESXSRV | Get-VM | Where { $_.PowerState -eq "poweredOn" }).Count 21 Write "Waiting for shutdown of $numvms VMs or until $timeleft seconds" 22 $Newtime = (Get-Date).TimeofDay - $Time 23 } until ((@($ESXSRV | Get-VM | Where { $_.PowerState -eq "poweredOn" }).Count) -eq 0 -or ($Newtime).Seconds -ge $waittime) 24 25 # Shutdown the ESX Hosts 26 $ESXSRV | Foreach {Get-View $_.ID} | Foreach {$_.ShutdownHost_Task($TRUE)} 27 28 Write-Host "Shutdown Complete"
Related videos on Youtube
Author by
mit
Updated on September 18, 2022Comments
-
mit over 1 year
I have ssh login to an ESXi 5 host.
All the guests have vmware tools running, so they can be shut down by the host properly.
I would like an equivalent for this menu option in vSphere client:
It shuts down all the guests automatically (if they have vmware tools running) and then the host itself.
Is there such a "smart" shutdown all command on the ESXi 5 commandline?
-
Admin over 12 yearsExact duplicate of serverfault.com/q/321909/50875
-
Admin over 12 yearsNot really a duplicate.This question asks for host shutdown as well as vms while the link above deals with vms only.To shutdown both, this script is one of the possible solutions: virtu-al.net/2010/01/06/…
-
Admin over 12 years@Sergei - That seems to be exactly what he's looking for -- Please post it as an answer :-)
-
Admin over 12 yearsHi, I think the question was closed that is why i just commented.If you mark my comment as useful, this is good enough for me , thank you :)
-