VMware Cloud Community

Howto monitor "Maintenance Mode" Task progress ?

Hello everyone,
I try to monitor a "Maintenance Mode" Progress and like to interact if the task get suckt. 

I found a very nice way to Monitor a random scheduling of a VM to a Host. Tanks to: @LucD 

$hosti=get-vmhost "myESXiHostname.fqdn.de"
$RandomHost=Get-Cluster $cluster|Get-VMHost|?{$_.Name -notmatch $hosti}|Get-Random
$t = move-vm $_.name -Destination $RandomHost -RunAsync
while('Running','Queued' -contains $t.State){
   Write-Progress -Activity 'Move VM to $RandomHost' -PercentComplete $t.PercentComplete
   $t = Get-Task -Id $t.Id

Now I try to to this with the Maintenace Mode to interact if it stops or is slow.

#$t=$esxcli.system.maintenanceMode.set.Invoke(@{vsanmode="ensureObjectAccessibility" ; enable=$true; timeout=100 }) # -RunAsync parameter not found 
$t = Set-VMHost -VMHost $_ -State "Maintenance" -VsanDataMigrationMode EnsureAccessibility -RunAsync
#while('Running','Queued' -contains $t.State -And $_.ConnectionState -eq "maintenance"){ #
while($_.ConnectionState -eq "maintenance"){
Write-Progress -Activity 'Creating VM' -PercentComplete $t.PercentComplete
sleep 2

The task is triggered, but I cannot get the information of the progress.

- Can I also include the "-RunAsync" parameter in $esxcli.system.maintenanceMode.set.Invoke posible ?
- How do I correctly interact with the Get-task (MS and vmware) to get the status of the maintenance mode ?

- Is there another way to see the "vmware" status of the internal task (e.g. in $T.ExtensionData.Info)?
- I had some conversion problems when converting the VMware Object values ​​to an [int32] after searching via Select-Object. A tip for me?

Thank you in advance !
- ESXI 6.7 U3 EP17
- vCenter 6.7 U3
- PowerShell 7.2.0-preview.2 (linux)


0 Kudos
1 Reply

You are hitting the difference between a Client Side Task and a Server Side Task.
Some cmdlets that use the RunAsync switch are converted into a Client Side Task.
The set Maintenance mode is one of those.

The drawback is that the Task object returned looks the same, but the Id property is a GUID-type identifier, not a Task object Id like a MoRef ( a pointer).
So you cannot do the Get-Task -Id cmdlet to find a fresh/updated representation of the task.

You can still use a Get-Task to find the updated Task object, by filtering the result of Get-Task by for example State, StartTime, Name ...
That should probably work in most cases.

I use code similar to the following to handle both types of Tasks.
$task contains the object returned by the cmdlet on which you used the RunAsync switch.


    {$_ -is [VMware.VimAutomation.ViCore.Impl.V1.VIObjectImpl]} {
        while('Running','Queued' -contains $task.State){
            Write-Progress -Activity 'Move VM to $RandomHost' -PercentComplete $t.PercentComplete
            $task = Get-Task -Id $task.Id
    {$_ -is [VMware.VimAutomation.Sdk.Util10.Task.ClientSideTaskImpl]} {
        while('Running','Queued' -contains $task.State){
            Write-Progress -Activity 'Move VM to $RandomHost' -PercentComplete $t.PercentComplete
            $task = Get-Task -Status $task2.State | where{$_.StartTime -eq $task.StartTime} 


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