Posting the whole script once more including the output. I am logged onto esxi host directly, i see machines are nicely being shutdown but host remains ON.
$esxName = '10.11.155.121'
$esx = Get-VMHost -Name $esxName
$esx.ExtensionData.Config.AutoStart.PowerInfo |
ForEach-Object -Process {
New-Object -TypeName PSObject -Property @{
VM = (Get-View -Id $_.Key -Property Name).Name
Order = $_.StartOrder
}
} | Sort-Object -Property Order,Name -Descending |
ForEach-Object -Process {
$vm = Get-VM -Name $_.VM
Shutdown-VMGuest -VM $vm -Confirm:$false
while($vm.ExtensionData.Runtime.PowerState -eq 'poweredOn'){
sleep 2
$vm.ExtensionData.UpdateViewData('Runtime.PowerState')
}
}
if((Get-VM -Location $esx).where{$_.PowerState -eq 'poweredoff'}.Count -eq 0){
Set-VMHost -VMHost $esx -State Maintenance
while(-not $esx.ExtensionData.Runtime.inMaintenanceMode){
sleep 2
$esx.ExtensionData.UpdateViewData('Runtime.inMaintenanceMode')
}
Stop-VMHost -VMHost $esx -Confirm:$false
}
else{
Write-Warning "One or more VMs did not stop"
}
When script is run:
Name Port User
---- ---- ----
10.11.155.121 443 root
OSFullName : Microsoft Windows Server 2016 (64-bit)
IPAddress : {10.11.110.240}
State : ShuttingDown
Disks : {Capacity:2144333824, FreeSpace:1983119360, Path:D:\, Capacity:63897071616, FreeSpace:19686383616,
Path:C:\}
HostName : BR110DC
Nics : {Network adapter 1:10.11.110.x}
ScreenDimensions : {Width=1280, Height=800}
VmId : VirtualMachine-2
VM : BR110DC
VmUid : /VIServer=root@10.11.155.121:443/VirtualMachine=VirtualMachine-2/
VmName : BR110DC
Uid : /VIServer=root@10.11.155.121:443/VirtualMachine=VirtualMachine-2/VMGuest=/
GuestId : windows9Server64Guest
ToolsVersion : 10.1.15
ExtensionData : VMware.Vim.GuestInfo
Client : VMware.VimAutomation.ViCore.Impl.V1.VimClient
GuestFamily : windowsGuest
OSFullName : Microsoft Windows Server 2016 (64-bit)
IPAddress : {10.11.155.133, fe80::d1a6:92a5:fa62:2999}
State : ShuttingDown
Disks : {Capacity:63898120192, FreeSpace:51471728640, Path:C:\}
HostName : Test01
Nics : {Network adapter 1:LAN}
ScreenDimensions : {Width=1920, Height=853}
VmId : VirtualMachine-5
VM : Test01
VmUid : /VIServer=root@10.11.155.121:443/VirtualMachine=VirtualMachine-5/
VmName : Test01
Uid : /VIServer=root@10.11.155.121:443/VirtualMachine=VirtualMachine-5/VMGuest=/
GuestId : windows9Server64Guest
ToolsVersion : 10.1.15
ExtensionData : VMware.Vim.GuestInfo
Client : VMware.VimAutomation.ViCore.Impl.V1.VimClient
GuestFamily : windowsGuest
OSFullName : Microsoft Windows Server 2016 (64-bit)
IPAddress : {10.11.110.100, 169.254.179.203}
State : ShuttingDown
Disks : {Capacity:2308838518784, FreeSpace:1053267234816, Path:D:\, Capacity:322119397376,
FreeSpace:278769627136, Path:E:\, Capacity:53159653376, FreeSpace:13294333952, Path:C:\}
HostName : BR110DFS.corp.kearnyfederalsavings.net
Nics : {Network adapter 1:10.11.110.x}
ScreenDimensions : {Width=1024, Height=768}
VmId : VirtualMachine-1
VM : BR110DFS
VmUid : /VIServer=root@10.11.155.121:443/VirtualMachine=VirtualMachine-1/
VmName : BR110DFS
Uid : /VIServer=root@10.11.155.121:443/VirtualMachine=VirtualMachine-1/VMGuest=/
GuestId : windows9Server64Guest
ToolsVersion : 10.1.15
ExtensionData : VMware.Vim.GuestInfo
Client : VMware.VimAutomation.ViCore.Impl.V1.VimClient
GuestFamily : windowsGuest
WARNING: One or more VMs did not stop
So after this warning nothing happens to the host, it remains on.