VMware Cloud Community
Viewaskew
Enthusiast
Enthusiast

PowerCLI Script to ID VMs on host, shut them down and power down host

Does anyone have a script to, as the title describes, list VMs running on a host (no cluster, HA DRS etc.), shutdown said VMs and then power off the host?

I found LUCd’s handy host power down script below but would like to extend it by being able to close off the VMs on the single host beforehand.

$names = "154.dc.local","164.dc.local","189.dc.local","199.dc.local"

connect-VIServer -Server $names -User root -Password Password1 | %{

  Get-VMHost -Server $_ | %{

$_.ExtensionData.ShutdownHost_Task($TRUE)

  }

}

Thanks if anyone can help

0 Kudos
3 Replies
LucD
Leadership
Leadership

Try like this.

I assume the VMware Tools are installed on the VM, otherwise the Stop-VMGuest would need to be replaced by a Stop-VM

$names = "154.dc.local","164.dc.local","189.dc.local","199.dc.local"

Connect-VIServer -Server $names -User root -Password Password1 | %{

  Get-VMHost -Server $_ | %{

    Get-VM -Location $_ | Stop-VMGuest -Confirm:$false

    $_.ExtensionData.ShutdownHost_Task($TRUE)

  }

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Viewaskew
Enthusiast
Enthusiast

Thanks LucD as always. One thing Ive noticed is that when running some of the command manually I don't have a Stop-VMHost, only ones I see are Stop-vApp, Stop-VM, Stop-VMHost, Stop-VMHostService. This is PowerCLI 5.5 Release 1.

Any idea why Stop-VMGuest isnt present as a command?

0 Kudos
LucD
Leadership
Leadership

Not really, I assume all the pssnapins are loaded ?

Do a

Get-PSSnapin -Name VMware* | Select Name


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos