Monitoring a Task Progression in PowerCLI

Sometimes you’ll be running a command which just takes far too long and you’re sat there wondering whether your code has crashed (unlikely because you’re awesome). But wouldn’t it be nice to know how far along your task is?

Using the below code and updating the task (coloured red), you should see some progression output in your code (just like looking at the vCenter Tasks).

In my example, I’ve done this for monitoring VMware Update Manager Compliance Check…

    try {
    $progress = Test-Compliance -Entity $cluster -RunAsync
    
        while ('Running','Queued' -contains $progress.State) {
            Write-Host "....... $($progress.PercentComplete)%"
            Start-Sleep 10
            $progress = Get-Task -Id $progress.Id
        }
    
        Write-Host "....... 100%"
        Start-Sleep -Seconds 5
    }
    catch [Exception] {
        Write-Host "Task Failed"
    }

This will query the vCenter every 10 seconds and report back what percentage this is until complete. Shown below…

....... 0%
....... 18%
....... 34%
....... 61%
....... 100%

This can be done for any task, such as Creating a New VM, Cloning a VM, etc. Note, the -RunAsync command must be used for this to work. This carries on with the script rather than waiting for your Task to complete.

Leave a Reply

Your email address will not be published. Required fields are marked *