Skip to content

Instantly share code, notes, and snippets.

@albal
Created November 23, 2018 14:36
Show Gist options
  • Select an option

  • Save albal/fcdbfc82c79f4bd5e4e7ef1832e88d9c to your computer and use it in GitHub Desktop.

Select an option

Save albal/fcdbfc82c79f4bd5e4e7ef1832e88d9c to your computer and use it in GitHub Desktop.
VMware PowerCLI PowerShell script to shutdown VMs that would be left running on an ESX host entering maintenance mode
# Connect to vCenter
Connect-VIServer -Server 192.168.0.10 -user administrator@vsphere.local -password <password>
# Get the HostId of the VM Host entering maintenance mode - assuming only one Host in a Data Center at a time
$hostid = Get-Task Running | where {$_.name -eq "EnterMaintenanceMode_Task"} | select ObjectId
# If there is a result then shutdown all the VMs that match the name given
if (-not ([string]::IsNullOrEmpty($hostid)))
{
# Get the VM Host name by the ObjectId
$esxhost = Get-VMHost | where {$_.Id -eq $hostid.ObjectId}
# Shutdown the VM given by the wildcard - in this case any machine begninning with Gluster running on that host
Get-VMHost -name $esxhost | Get-VM Gluster* | Shutdown-VMGuest -confirm:$false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment