PowerShell - asynchronous operation?

Status
Not open for further replies.

RojBlake

Ars Legatus Legionis
48,129
Subscriptor
I'm writing a script to remove VMware snapshots and I want to be able to run, say, 4 removals at a time so as to avoid putting too much load on the SAN.

The snapshot removal command - Remove-Snapshot - has a '-RunAsync' option, so I can make it execute on one VM and then immediately move on to the next VM. This is great, but if I feed it my list of 40+ VMs then it's going to immediately kick off 40 snapshot removal operations. I can't figure out how to make it stop after 4 executions and then wait until one finishes before adding the next one.

In other words, I can't figure out a way to make the script do: remove snapshot but keep an eye on it and report back when it's done.

Any ideas?
 

skel_

Seniorius Lurkius
17
I think KallDrexx has the most apt suggestion so far, but there does exist a concept in PowerShell called Workflows which you may want to take a look at. It's not documentented all that well in my opinion, also very limited when you want to do anything that's remotely complicated, and it will be removed in the new version of PowerShell: PowerShell Core 6.

If you have a simple command that needs to be run asynchronously, then it might just be fine, and if you're interested then I can get you a code example tomorrow as it took me longer than I wanted to get it to work the way I wanted; don't want anyone else to needlessly spend time on it.

In the end opted to use Jobs for the complex scenario I had as anything with several functions and function calls will cause you a lot of headaches with Workflows.
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
From https://www.vmware.com/support/develope ... pshot.html

Indicate that the command returns immediately without waiting for the task to complete. In this mode, the output of the cmdlet is a Task object. For more information about the -RunAsync parameter run " help About_RunAsync" in the vSphere PowerCLI console.

So in c# this would be pretty trivial, and I would be somewhat tempted to drop to embedded c# as expressing the desire to have N operations and only have K of them active is really simple, I don't know how to easily express tat in powershell.

But you can definitely wire up to the tasks completed event (or poll on them) and issue a new request as one completes.
(Task.WaitAny will do that in c#)
 

RojBlake

Ars Legatus Legionis
48,129
Subscriptor
Never done anything in powershell, but can the Remove-Snapshot do a callback to something when it's done? Or using the powershell job, create your job to include a callback after the job completes which then can have notify your running job it's done.
This pointed me in the right direction* - there's a VMware commandlet called Get-Task that holds info about running tasks, so it's a simple matter of initiating 4 snapshot removal jobs and then monitoring that task list for the completion of one of them before adding a new one. It works perfectly!

In case anyone's interested, here's an example for deleting all snapshots on all connected vCenters:
Code:
$maxTasks = 4
$vms = Get-VM | Sort Name
$snaps = $vms | Get-Snapshot
$i = 0
while($i -lt $snaps.Count)
    {
    Remove-Snapshot -Snapshot $snaps[$i] -RunAsync -Confirm:$false
    $tasks = Get-Task -Status "Running" | ?{$_.Name -eq "RemoveSnapshot_Task"}
    while($tasks.Count -gt ($maxTasks-1))
        {
        sleep 30
        $tasks = Get-Task -Status "Running" | ?{$_.Name -eq "RemoveSnapshot_Task"}
        }
    $i ++
    }
Thanks, guys!

* And then I see later that ShuggyCoUk referenced the specific technique I ended up using.
 
Status
Not open for further replies.