Automation

 View Only
  • 1.  How do I view and monitor currently running tasks?

    Posted Apr 24, 2009 06:08 PM

    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"

    }



  • 2.  RE: How do I view and monitor currently running tasks?

    Posted Apr 24, 2009 06:35 PM

    What cmdlet or method did you use to start the task ?



  • 3.  RE: How do I view and monitor currently running tasks?

    Posted Apr 24, 2009 06:59 PM

    Actually I found the answer in one of your other posts Luc. Thanks for being so helpful! :smileyhappy:

                                                    1. 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

    1. Write-Host "Server" $serv

    $FolderID = Get-Folder -Name $foldername #-Server $serv

    1. Write-Host "FolderID" $FolderID

    $targetfolder = get-view ($FolderID).ID

    1. Write-Host "targetfolder" $targetfolder.ID

    $destdsview = get-view (get-datastore -name $destdatastr).id

    1. 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

    1. Write-Host "State: " $task.Info.State

    while ($task.Info.State -eq "running" -or $task.Info.State -eq "queued")

    {

    $task = Get-View $clntask

    1. Write-Host "Waiting"

    }

    $clntask | out-file $PHP_CloneLog -Append -Encoding "ASCII"

    ############################################################################

    }



  • 4.  RE: How do I view and monitor currently running tasks?
    Best Answer

    Posted Apr 24, 2009 07:04 PM

    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.



  • 5.  RE: How do I view and monitor currently running tasks?

    Posted Apr 27, 2009 07:39 AM

    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



  • 6.  RE: How do I view and monitor currently running tasks?

    Posted Apr 27, 2009 09:16 AM

    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 ?



  • 7.  RE: How do I view and monitor currently running tasks?

    Posted Apr 27, 2009 09:37 AM

    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



  • 8.  RE: How do I view and monitor currently running tasks?

    Posted Apr 27, 2009 09:55 AM

    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.