4

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?

JohannesM
  • 166
  • 2
  • 13
mit
  • 1,844
  • 6
  • 29
  • 42
  • Exact duplicate of http://serverfault.com/q/321909/50875 – gWaldo Jan 14 '12 at 03:13
  • 3
    Not 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: http://www.virtu-al.net/2010/01/06/powercli-shutdown-your-virtual-infrastructure/ – Sergei Jan 15 '12 at 12:25
  • 1
    @Sergei - That seems to be exactly what he's looking for -- Please post it as an answer :-) – voretaq7 Jan 25 '12 at 23:19
  • Hi, 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 :) – Sergei Jan 27 '12 at 19:10

1 Answers1

2

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"
mit
  • 1,844
  • 6
  • 29
  • 42