VMware Cloud Community
mrtgokul
Contributor
Contributor

PowerCLI Maintenance Mode

Hi Team, 

I am trying to Put a Host into Maintenance Mode which has both Powered on and Powered off VMs using 

"Set-VMHost -VMHost $Hostname -State Maintenance " Cmdlet. Host is moving to MM by migrating the Powered ON VMs to another host but its not migrating the Powered Off VMs. But when I try to do the same using GUI, I have an option to migrate the Powered OFF VMs as well to another host in the Cluster. 

Can we achieve the same via PowerCLI which is to migrate all the VMs both Powered ON and Powered OFF vms to another host and then place into MM.

Can someone help me in this?

Reply
0 Kudos
7 Replies
LucD
Leadership
Leadership

Did you try the -Evacuate switch?


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

Reply
0 Kudos
Macleud
Enthusiast
Enthusiast

Hi.

I'm using alternate version.

It will also unmount vmtools from vm on the host.

$EsxName= "Esx_02"

$Esx = Get-View -ViewType HostSystem -filter @{"name"="$EsxName"}
$Vm = Get-View -ViewType VirtualMachine -SearchRoot $Esx.MoRef
$Vm | Where {$_.Runtime.ToolsInstallerMounted} | % {$_.UnmountToolsInstaller()}
$spec = New-Object VMware.Vim.HostMaintenanceSpec
$timeout = 0
$evacuatePoweredOffVMs = $true
$taskSDK = $esx.EnterMaintenanceMode_task($timeout,$evacuatePoweredOffVMs,$spec)
$task = Get-task -status Running | Where {$_.id -match "$($taskSDK.ToString())"}
Wait-Task -Task $task

Reply
0 Kudos
mrtgokul
Contributor
Contributor

That worked, Thanks LucD 😊

Reply
0 Kudos
mrtgokul
Contributor
Contributor

Thank you Macleud 😊

Reply
0 Kudos
mrtgokul
Contributor
Contributor

Hi @LucD On my previous reply, I tried it on a different host which was not having powered off VMs. But now when I tried the Evacuate Switch, it puts the host into MM but its not moving the Powered Off VMs to other host.

Reply
0 Kudos
LucD
Leadership
Leadership

Afaik, the cmdlet does not have the option to evacuate powered off VMs.
For that you have to use the API.
Something like this

$esxName = 'MyEsx'

$esx = Get-VMHost -Name $esxName

$timeout = 0    # No timeout
$evacuatePoweredOffVms = $true
$spec = New-Object -TypeName VMware.Vim.HostMaintenanceSpec
$spec.VsanMode = New-Object -TypeName VMware.Vim.VsanHostDecommissionMode
$spec.VsanMode.ObjectAction = [VMware.Vim.VsanHostDecommissionModeObjectAction]::noAction

$esx.ExtensionData.EnterMaintenanceMode($timeout, $evacuatePoweredOffVms, $spec)


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

Reply
0 Kudos
mrtgokul
Contributor
Contributor

This method worked, Thank you so much 😊

Reply
0 Kudos