VMware Cloud Community
tayky
Contributor
Contributor
Jump to solution

Retrieve VM cloning status

Hi all, does anyone have any power cli sample to retrieve the status of vm cloning without the need to access thru the vSphere Client ?

0 Kudos
1 Solution

Accepted Solutions
CRad14
Hot Shot
Hot Shot
Jump to solution

Why not just...

get-task | ?{$_.name -like "CloneVm_Task"}

Conrad www.vnoob.com | @vNoob | If I or anyone else is helpful to you make sure you mark their posts as such! 🙂

View solution in original post

0 Kudos
14 Replies
LucD
Leadership
Leadership
Jump to solution

Afaik that is not possible.

You could look at the events of the target host and filter out the events related to the creation of the new VM, but that will not give you the status of the cloning task.

Out of curiousity, how do you start the cloning process without a connection to the vCenter Server ?


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

0 Kudos
tayky
Contributor
Contributor
Jump to solution

Hi,

I actually make use of the  web service SDK thru my web to pass in the parameters to connect to the vCenter Server for the cloning so that i do not require to install vSphere Client on different pc.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can't you access the TaskManager in a similar way and interrogate the recentTask property ?

That is in fact what the Get-Task cmdlet is doing.


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

0 Kudos
tayky
Contributor
Contributor
Jump to solution

Is there any sample code for it?

0 Kudos
CRad14
Hot Shot
Hot Shot
Jump to solution

Why not just...

get-task | ?{$_.name -like "CloneVm_Task"}

Conrad www.vnoob.com | @vNoob | If I or anyone else is helpful to you make sure you mark their posts as such! 🙂
0 Kudos
tayky
Contributor
Contributor
Jump to solution

Hi CRad14,

Thanks very much for your help. I'm able to get the cloning status for the VM now.

Seems like i have alot more to learn on the powercli script.

0 Kudos
CRad14
Hot Shot
Hot Shot
Jump to solution

My pleasure! Have a good one!!

Conrad www.vnoob.com | @vNoob | If I or anyone else is helpful to you make sure you mark their posts as such! 🙂
0 Kudos
The0ray
Contributor
Contributor
Jump to solution

So to take this further, I'd like to monitor a running task that could take over 5hrs. Currently this provides a percentage complete:

      Get-Task | ? {$_.Description -eq "Relocate virtual machine" -and $_.State -eq "Running"} | ft -a

Could someone expand to make some type of "while" loop or once task hits 99 or 100%, then send an email?              

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try something like this

Get-Task | where {$_.Description -eq "Relocate virtual machine" -and "Running","Success" -contains $_.State -and $_.PercentComplete -ge 70} | %{
  Send-MailMessage -SmtpServer mailserver.domain -Subject "Job $($_.Id) at $($_.PercentComplete) percent" -To me@domain -From me@domain
}


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

0 Kudos
The0ray
Contributor
Contributor
Jump to solution

Yea, that's great Luc! Now can you advise how to make this one-liner actively sit and monitor the progress then send email once it hits 98% complete or equals success? Something like "do, while"?

0 Kudos
The0ray
Contributor
Contributor
Jump to solution

Hmm, getting closer to what I want, but now I need to either use Wait-task or some type of sleep timer? Ideas please??

Get-Task | ? {$_.Description -eq "Relocate virtual machine"}; do {Get-Task | ? {$_.Description -eq "Relocate virtual machine" -and $_.State -eq "Running"}} until ($_.State -eq "success")

Produces:

Name                           State      % Complete Start Time   Finish Time
----                           -----      ---------- ----------   -----------
RelocateVM_Task                Running            30 09:04:26 AM
RelocateVM_Task                Running            30 09:04:26 AM
RelocateVM_Task                Running            30 09:04:26 AM
RelocateVM_Task                Running            30 09:04:26 AM
RelocateVM_Task                Running            30 09:04:26 AM
0 Kudos
The0ray
Contributor
Contributor
Jump to solution

OK, this is working and providing status back to the screen every 5mins. Still not precisely what I need, I need a way to generate email when completed.

Can I simply add this to the end of my line below?

| %{Send-MailMessage -SmtpServer mailserver.domain -Subject "Job $($_.Id) at $($_.PercentComplete) `
percent" -To me@domain -From me@domain

Get-Task | ? {$_.Description -eq "Relocate virtual machine"}; do {Get-Task | ? {$_.Description -eq "Relocate virtual machine" -and $_.State -eq "Running"}} until (start-sleep -seconds 600) ($_.State -eq "success")

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Would this do the trick ?

$tasks = Get-Task | where {$_.Description -eq "Relocate virtual machine"}
while($tasks){
  sleep 5  
  $tasks = Get-Task | where {$_.Description -eq "Relocate virtual machine" -and $_.State -eq "Running"}
}

Get-Task | where {$_.Description -eq "Relocate virtual machine" -and "Success","Error" -contains $_.State} | %{
  Send-MailMessage -SmtpServer mailserver.domain -Subject "Job $($_.Id) at complete, status $($_.State)" -To me@domain -From me@domain
}

In the loop the script waits for 5 seconds before fetching the tasks again.

The loop continues till all tasks are either completed or in error.

Then it sends an email per task that was completed or errored out.


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

0 Kudos
Joshi_Sai_Chait
Contributor
Contributor
Jump to solution

Hi tayky & CRad14,

Even Iam Looking For solution of the same Problem( Knowing the Status(%) of VM Clone ),and thanks for your answer (CRad14) ,but here i  have one Question,

Iam Creating Multiple VMs from  Template using the Powercli with -RunAsync Parameter,then how can i differentiate  the CloneVM_Task ,i.e which task represents the status of which  VM ?..

Iam using New-VM Command let to create a new VM using the Template and One OSCustomizationSpec file (Unattended File) which is already Created.

but if different users are connected from different places to VCenter  and created VMs then how can i diferentiate the Which Task Status indicates the Which VM creationbecause all the tasks are Having Same Name "VMClone_Task"

Thanks In Advance

0 Kudos