I need to be able to monitor a running task and this isn't working:
while ($clntask.Info.State -eq "running" -or $clntask.Info.State -eq "queued")
{
$clntask = Get-View $clntask.MoRef
Write-Host "Waiting"
}
I'll assume you used a SDK method to start the task.
I'll take the ExitMaintenanceMode_Task method as an example.
*) the SDK ...._Task methods return a MoRef (a kind of pointer) to a Task object.
*) to get at the properties of the task object we use the Get-View cmdlet to get a copy of the actual Task object
*) we check the property that gives the status of the task. The status can be: error, queued, running or success.
*) If the status is "running" or "queued" we wait 2 seconds and get a new copy of the Task object. The VI will not update the properties of our copy of the Task object
*) When the status is not "running" or "queued" we exit the while loop
...... $taskMoRef = $esx.ExitMaintenanceMode_Task(0) $task = Get-View $taskMoRef while ($task.Info.State -eq "running" -or $task.Info.State -eq "queued") { sleep 2 $task = Get-View $taskMoRef }
Just saw your reply. My remarks are still valid.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
What cmdlet or method did you use to start the task ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Actually I found the answer in one of your other posts Luc. Thanks for being so helpful!
Change the ###### below to your VirtualCenter Servers FQDN
Connect-VIServer -Server ###### -Protocol https | out-file $PHP_CloneLog -Append -Encoding "ASCII"
Function CreateNewClone #Must be passed 6 agruments (<VM to Clone>,<New Clone Name>,<Server[Host/VC]>,<DataStore Name>,<Folder>,<ResourcePool>)
{
$vm2c = $args[0] #Name of the virtual to be cloned.
$destcln = $args[1] #Name of the clone to be made.
$erver = $args[2] #Name of the host or virtual center server.
$destdatastr = $args[3] #Name of the destination datastore to which the new clone will be deployed (Optional).
$foldername = $args[4] #Name of the destinaion folder for the clone.
$resourcepool = $args[5] #Name of the destination Resource Pool for the clone.
"{0} Initializing clone task for: $vm2c >>-->> $destcln" -f ::Now | out-file $PHP_Clonelog -Append -Encoding "ASCII"
" Using DataStore named: $destdatastr" -f ::Now | out-file $PHP_Clonelog -Append -Encoding "ASCII" " Using ResourcePool named: $resourcepool" -f ::Now | out-file $PHP_Clonelog -Append -Encoding "ASCII"
$serv = Connect-VIServer -Server $erver -Protocol https
Write-Host "Server" $serv
$FolderID = Get-Folder -Name $foldername #-Server $serv
Write-Host "FolderID" $FolderID
$targetfolder = get-view ($FolderID).ID
Write-Host "targetfolder" $targetfolder.ID
$destdsview = get-view (get-datastore -name $destdatastr).id
Write-Host "destdsview" $destdsview.ID
$destpool = Get-View (Get-ResourcePool -Name $resourcepool).id
$VMCloneSpec = New-Object VMware.Vim.VirtualMachineCloneSpec
$VMCloneSpec.Location = New-Object VMware.Vim.VirtualMachineRelocateSpec
$VMCloneSpec.location.datastore = $destdsview.moref
$VMCloneSpec.location.pool = $destpool.moref
$VMCloneSpec.powerOn = $false
$VMCloneSpec.template = $false
$clntask = (Get-View (Get-VM -Name $vm2c).ID).CloneVM_Task($targetfolder.MoRef, $destcln, $VMCloneSpec)
$task = Get-View $clntask
Write-Host "State: " $task.Info.State
while ($task.Info.State -eq "running" -or $task.Info.State -eq "queued")
{
$task = Get-View $clntask
Write-Host "Waiting"
}
$clntask | out-file $PHP_CloneLog -Append -Encoding "ASCII"
############################################################################
}
I'll assume you used a SDK method to start the task.
I'll take the ExitMaintenanceMode_Task method as an example.
*) the SDK ...._Task methods return a MoRef (a kind of pointer) to a Task object.
*) to get at the properties of the task object we use the Get-View cmdlet to get a copy of the actual Task object
*) we check the property that gives the status of the task. The status can be: error, queued, running or success.
*) If the status is "running" or "queued" we wait 2 seconds and get a new copy of the Task object. The VI will not update the properties of our copy of the Task object
*) When the status is not "running" or "queued" we exit the while loop
...... $taskMoRef = $esx.ExitMaintenanceMode_Task(0) $task = Get-View $taskMoRef while ($task.Info.State -eq "running" -or $task.Info.State -eq "queued") { sleep 2 $task = Get-View $taskMoRef }
Just saw your reply. My remarks are still valid.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Just to mention thaht after you sleep 2 seconds instead of calling $task = Get-View $taskMoRef you could just update the view by doing $task.UpdateViewData() which is faster. If you are interested only in the State property of the task you could even optimize the above call as a matter of time by specifying only the State property to update: $task.UpdateViewData("Info.State")
\Yavor
Yavor, very useful information, thanks !
This is apparently a new method since I can't seem to find it in the API Reference v2.5.
Any pointers where this new method is explained ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Luc,
Actually UpdateViewData() is not an API method. It is a custom VITK method designed to work with view objects. There is a little something mentioned about how to update view objects in the http://www.vmware.com/support/developer/windowstoolkit/wintk10/doc/viwin_devg.pdf document. We'll include this one in the installation folder for the upcomming release as alot of people asked about it on vmworld europe 2009. The method is pretty simple so you don't need much of a documentation about it but if you need any further info I'll be happy to provide it .
\Yavor
Yavor, thanks for the pointer.
Just realised that the ViewBase is also documented in the CHM file that comes with VITK v1.5.
It's even in the VITK Program folder as VI API .NET Namespace Reference.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference