I successfully figured out that there is a property called ObjectId on Get-Task that matches the VM Id.
You may keep track of the tasks by matching the VM Id and the Task ObjectId. In my case I was exporting a VM to OVA template.
PS C:\> $vm = Get-VM -Name "vm1"
PS C:\> Get-Task | Where { $_.ObjectId -eq $vm.Id }
Name State % Complete Start Time Finish Time
---- ----- ---------- ---------- -----------
Export OVF template Running 5 10:37:14
PS C:\>
I recorded the VM Id to the hashtable and then matched that with the task.
Function ExportOVA ($VAppName,$VMtoExport)
{
$VMtoExp = Get-VM -Name $VMtoExport
$null = Export-Vapp -Name $VAppName -VM $VMtoExp -Destination $OVAStaging -Format Ova -ErrorAction Stop -RunAsync
$exportTab.Add(($VMtoExp).Id, $VAppName)
}
while($runningTasks-gt0){
Get-Task | % {
If ($exportTab.ContainsKey($_.ObjectId) -And $_.State -eq "Success") {
...
}
}
}