Hello guys. I am trying to create an automation script to clone and export my VMs as OVA format, using New-VM and Export-Vapp cmdlets. It works fine, except that I am using on both cmdlets the -RunAsync switch, that way I run more than one task at time.
On the script logic, I keep track of the TASK ID in a hashtable for each of the New-VM and Export-Vapp commands, that way I can figure out when one task, for example, cloning a VM finishes, and starting an Export-Vapp as soon as the clone completes.
When running New-VM -RunAsync switch, it does return the TASK ID like "Task-task-337834", but when running Export-Vapp with the -RunAsync switch, it returns the TASK ID like "a2a25670-314f-4b40-8eeb-25a0898e0099", so I can't figure out when one Export task has finished. I need that information, because I am limiting the Jobs to a certain concurrent job limit.
To clarify, try the following command:
PS C:\> $task = (Export-Vapp -Name "vm1" -VM "vm1-clone" -Destination "F:\" -Format Ova -ErrorAction Stop -RunAsync)
PS C:\> $task
Name State % Complete Start Time Finish Time
---- ----- ---------- ---------- -----------
Running 10 11:48:06
PS C:\> $task.id
a2a25670-314f-4b40-8eeb-25a0898e0099
PS C:\> $task.percentcomplete
14,8
PS C:\> $task.percentcomplete
15,6
PS C:\> Get-Task -Id a2a25670-314f-4b40-8eeb-25a0898e0099
Get-Task : 18/04/2014 12:20:39 Get-Task The identifier a2a25670-314f-4b40-8eeb-25a0898e0099 resulted in no ob
jects.
At line:1 char:9
+ Get-Task <<<< -Id a2a25670-314f-4b40-8eeb-25a0898e0099
+ CategoryInfo : ObjectNotFound: (:) [Get-Task], VimException
+ FullyQualifiedErrorId : Client20_OutputTracker_ReportNotFoundLocators_LocatorNotProduced,VMware.VimAutomation.Vi
Core.Cmdlets.Commands.GetTask
PS C:\>
PS C:\p> Get-task | Where { $_.Name -eq "Export OVF template" } | % { $_.Id }
Task-task-337834
How do I get the TASK ID like "Task-task-337834" when running the Export-Vapp cmdlet?
When running the New-VM it does return the TASK ID successfully:
PS C:\> $task = (New-VM -Name "vm1-clone" -VM "vm1" -VMHost server1.local -Datastore "Datastore-1" -DiskStorageFormat Thin -ErrorAction Stop -RunAsync)
PS C:\> $task
Name State % Complete Start Time Finish Time
---- ----- ---------- ---------- -----------
CloneVM_Task Running 0 12:16:56
PS C:\Users\cs55603\Desktop\migravm\bkp> $task.id
Task-task-337836
PS C:\Users\cs55603\Desktop\migravm\bkp> $task.percentcomplete
0
PS C:\> Get-Task -Id Task-task-337836
Name State % Complete Start Time Finish Time
---- ----- ---------- ---------- -----------
CloneVM_Task Success 100 12:16:56 12:18:02
PS C:\>
I tried both PowerCLI version 5.1 and now 5.5 R2, same result.