Needed to shutdown some hosts due to power work happening in one of our buildings, so therefore needed make sure all the various teams affected? (effected? can never remember which to use) shutdown their VMs so I could shutdown the hosts. So coming closer to power down time, still 40 odd VMs still powered on. Not good, so how can I shut them all down quickly without having to do them all manually.
Powershell.
First off I know the location that is going to be affected, a subset of the Virtual Infrastructure (VI), so get the object.
$location = Get-Datacenter -Name <mylocation>
Then to get a list of the VMs that are currently still powered on in this location.
get-vm -Location $location | Where-Object { $_.PowerState -eq "PoweredOn" } | sort
Now initiate a nice Guest OS Shutdown (provided they have VM Tools installed).
get-vm -Location $location | Where-Object { $_.PowerState -eq "PoweredOn" } | sort | shutdown-vmguest
Errors will be generated for VMs that do not have VM tools installed:
Shutdown-VMGuest : 2/12/2009 6:01:13 PM Shutdown-VMGuest F4B48556-AF1D-4D25-8E1E-B3F8C66D94AB Operation “Shutdown VMGuest” failed for VM “myVM” for the following reason: Operation failed since VMware tools are not running in this virtual machine.
At line:1 char:96
+ get-vm -Location $dc | Where-Object { $_.PowerState -eq “PoweredOn” } | sort | shutdown-vmguest <<<<
These can be safely ignored.
Then for the remaining VMs, hard power them off.
get-vm -Location $location | Where-Object { $_.PowerState -eq "PoweredOn" } | sort | Stop-VM
					