VMware Cloud Community
Fourmica
Contributor
Contributor

Waiting for a template clone to finish deploying

I am working on an automated deployment script that creates VMs from a template. I've figured out most of the OS Customization stuff, even the NIC stuff! (Mostly.) But I'm stuck on getting my script to wait for a VM to finish cloning from the template.

I'm using the command:

New-VM -name $myTestServer -Template $myTestTemplate -VMHost $myVMhost -OSCustomizationSpec $mySpec -datastore $myDatastore

I can then see in vCenter that the machine is cloning, with a progress bar. However, the command doesn't wait for this; it just moves on.

I tried:

$vmCloneTask = New-VM -name $myTestServer -Template $myTestTemplate -VMHost $myVMhost -OSCustomizationSpec $mySpec -datastore $myDatastore -RunAsync

Wait-Task -task $vmCloneTask

That didn't work either; the task is treated as being complete and the script continues immediately.

What's interesting is that the next command is "Start-VM $myTestServer". Instead of erroring out, it actually waits until the machine is done cloning and then starts it. So, in practice, I'm getting the functionality I'm looking for... but it doesn't seem like the right way to do it.

Any suggestions? PowerCLI 5.0.0 build 3501, Powershell 2.0, vCenter Server 5.0.0 build 755629, ESX 4.1.0 build 702113.

Thanks,

Josh

0 Kudos
3 Replies
LucD
Leadership
Leadership

WIth the RunAsync parameter the cmdlet immediatly returns.

In that case you have to use the Get-Task cmdlet to check the status of the background task.

But since you are using OSCustomization, there will be a sysprep step that will run on the first boot of the VM.

To sync to that process, you will have to find another way of checking if the task is complete.

If for example, the VM joins a domain, you could test on domain membership to determine if the deployment is complete.

The easiest solution, is to just use a Sleep for a number of seconds.

By testing you should be able to tell roughly how long the deployment, poweron and the sysprep takes.

But like I said that is the easiest method, and not fool-proof.


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

Fourmica
Contributor
Contributor

Thanks, LucD. That's exactly what I'm doing, actually; after Start-VM returns, the sysprep kicks off, and I do-while query the domain controller looking for the machine having joined the domain to know when sysprep is done. I'll play with Get-Task and see what I can come up with. The time to clone a template varies dramatically across our enterprise (many datacenters with differing loads) so I'm leery to use sleep to wait for the template to finish.

BTW, this is at least the second time you've helped me out, thank you. I've learned a lot from your posts and expertise!

Thanks,

Josh

0 Kudos
LucD
Leadership
Leadership

You're welcome, glad to help.

When you use the RunAsync parameter, the New-VM will return a Task object.

Save that, it will allow you to test which of the tasks returned by Get-Task is the one you should looking at.

In these Task objects you will see a Status property that you can test for Running, Success or Error.

Use the Name and ObjectId for determing which is the Task object to look at.


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

0 Kudos