I’ve been working on a project that uses the -runasync parameter on the move-vm cmdlet. The obvious problem with running jobs asynchronously is that you lose the ability to determine the status of whatever task as soon as you submit it.
I ran into a challenge on this project where I needed to validate some details from the job that was submitted asynchronously. It was more difficult and annoying than I thought it would be, but I managed to get a function together that can get a bunch of information.
The script assumes a pre-existing connection to vCenter and accepts the parameters below.
- Status_to_check – with a default value of “Initiate vMotion Receive operation”
- vCenter – the FQDN of the vCenter server. I added this in case you are connected to multiple vCenter servers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
function Test-vCenterTasks { param( $status_to_check = "Initiate vMotion receive operation", $vcenter ) $tasks = get-task -Server $vcenter $vmotions=$tasks | ? { $_.name -eq $status_to_check} $rs = @() foreach ($vmotion in $vmotions) { $vminflight = $vmotion.ExtensionData.Info.EntityName $vmobj = get-vm $vminflight -Server $vcenter $cluster = $vmobj | Get-Cluster -Server $vcenter $obj = New-Object –typename PSObject $obj | Add-Member –membertype NoteProperty –name VM_Name –value $vmobj.name $obj | Add-Member –membertype NoteProperty –name VM_Cluster –value $cluster.name $obj | Add-Member –membertype NoteProperty –name StartTime –value $vmotion.StartTime $obj | Add-Member –membertype NoteProperty –name Progress –value $vmotion.PercentComplete $obj | Add-Member –membertype NoteProperty –name Description –value $vmotion.Description $rs += $obj } return $rs } Test-vCenterTasks -vcenter "vcenter-server-fqdn" |