Here is a small function that you can use to wait for a task to complete, it basically loops until the state is success OR if it errors out. Remember, when you get a task back, you only have access to it for ~10min, once it's passed, even if you have the taskRef handle, you can't query it. You'll need to use the taskManager and CreateCollectorForTasks to collect the specific task and query it's status and basically block until it's complete.
my $task = ...
my $success = "suceess!";
&getStatus($task,$sucess);
sub getStatus {
my ($taskRef,$message) = @_;
my $task_view = Vim::get_view(mo_ref => $taskRef);
my $taskinfo = $task_view->info->state->val;
my $continue = 1;
while ($continue) {
my $info = $task_view->info;
if ($info->state->val eq 'success') {
print $message,"\n";
return $info->result;
$continue = 0;
} elsif ($info->state->val eq 'error') {
my $soap_fault = SoapFault->new;
$soap_fault->name($info->error->fault);
$soap_fault->detail($info->error->fault);
$soap_fault->fault_string($info->error->localizedMessage);
die "$soap_fault\n";
}
sleep 5;
$task_view->ViewBase::update_view_data();
}
}
=========================================================================
William Lam
VMware vExpert 2009
VMware ESX/ESXi scripts and resources at:
Twitter: @lamw
VMware Code Central - Scripts/Sample code for Developers and Administrators
VMware Developer Community

If you find this information useful, please award points for "correct" or "helpful".