VMware Cloud Community
BCGen
Contributor
Contributor

Looking for active tasks on a machine before deleting / replacing ...

I'm trying to figure out a way to determine if there are active tasks running against a template before deleting and recreating a template.

Basically, I'm trying to automate the refreshing of a template.  If that template is already being used in a cloning operation, I don't want to delete it (and probably can't anyway).  I want to wait basically clone a machine to a temp machine and convert that to a template, then check to see if all operations are done on the current template.

If operations are in progress, then wait.  Once existing operations are done (of if there are no operations in progress), I will delete the existing template, rename the 'temp' template to the name of the deleted template and off we go ...

So, I'm looking for some architecture/implementation advice on the best way to do this?   Should I use get-task and somehow filter against a specific VM to see what operations may be in progress on the existing template?  Should I just try to delete it and if it can't because it's already in use, trap this and sleep a while, then try again?

Thanks in advance for any ideas out there!  Much appreciated!

0 Kudos
4 Replies
LucD
Leadership
Leadership

In the Task object returned by cmdlet that you run with the RunAsync switch, you can find the template that is used.

Something like this allows you to wait till the Template isn't used anymore.

$templateName = "MyTemplate"
$template = Get-Template -Name $templateName
$esx = Get-VMHost MyEsx

New-VM -Name TestVM -Template $template -VMHost $esx -RunAsync

While (Get-Task -Status Running |
 
Where {$_.Name -eq "CloneVM_Task" -and $_.ExtensionData.Info.EntityName -eq $templateName}){
 
Write-Output "Template $templateName is used"
 
Sleep 5
}

Write-Output "Template $templateName is not used anymore"


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
rsoc
Enthusiast
Enthusiast

If you just want to check if there is an active task on an object like a template you can do the following:

$id = (get-template $yourTemplate).id

get-task | where{(($_.state -like "running") -and ($_.objectid -like $id))}

If you want to incorporate this as a check in a script you could do the following:

$id = (get-template $yourTemplate).id

If (get-task | where{(($_.state -like "running") -and ($_.objectid -like $id))}){

     write-host "there is an active task on $yourTemplate which is preventing this script from acting on it"

     start-sleep -s 10

     exit

}

If you want to incorporate this check in a script a loop to wait for the task to finish you could do the following:

$i=0

$id = (get-template $yourTemplate).id

while(get-task | where{(($_.state -like "running") -and ($_.objectid -like $id))}){

     start-sleep -s 10

     $i += 1

     If($i -ge 60){

          exit

     }

}

Do your action here because this while loop will run until there are no running tasks for $yourTemplate. This loop will simply exit the script after 10 minutes which could be configured for your purposes.

0 Kudos
BCGen
Contributor
Contributor

Thanks for the great info guys --

In further testing, it seems that if I issue a powercli command, that command will basically queue up until the entity is free.

For example, say I'm cloning a template to a new machine, and while cloning I issue a "set-template -template TestBaseline -Name NewName -Confirm:$false" command, it seems to wait until the previous process is done and then executes the process.  In fact, in the GUI, I see the task waiting and processing after the entity frees up.

So maybe I don't actually even have to check to see if a task is in progress??  The looming question in my mind is whether there is some sort of process timeout in CLI -- in other words, if there is a long running clone process, for example, would the pending task timeout at some point -- or will it happily wait indefinitely.

0 Kudos
LucD
Leadership
Leadership

It should wait, but I assumed that you were using the RunAsync switch.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos