How to shutdown the Host over ssh on ESXi 5 so it shuts down the guests properly

powerclishellvmware-esxi

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:

enter image description here

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?

Best Answer

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"