VMware Cloud Community
casaub
Enthusiast
Enthusiast
Jump to solution

vCSA 6.7 scheduled task - clone VM (to same ESXi host, same VMFS, deleting previous clone)

Hello,
we are using vCSA 6.7. Noticed that it is not possible to schedule tasks for cloning VM to same folder, same ESXi host and same VMFS datastore (the largest datastore of the ESXi host). As I understand the second cloning will fail because of the naming - because a clone with same name already exists.


As a solution a script would be needed - or is there a solution for this inside vCSA in the meantime? This discussion is providing a script, but it is from 2017:

https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/Guide-for-the-script-need-to-take-clon...

 

This script has to be run outside of the vCenter, as I understand.

 

How would a script look like which creates this scheduled task inside the vCSA? This scheduled task is supposed to achieve the same as the script mentioned in the discussion and additionally that task should place the clone on the same ESXi host where the VM is running on (not on a random ESXi, worried about 'Get-Random').


0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

I'm afraid that is a shortcoming of how Scheduled Tasks in the VCSA work.
It would be the same if you create the Scheduled Task via the Web Client.
The API that is used by the Clone_VM method expects a name, unfortunately you can't use variables in that name.
So it will always be the same.

When I create a clone every day, I first have another Scheduled Task that removes the clone of the previous day.
Then the same name is no issue.

The sample script uses the Pool to define where the clone should be located.
But you can also specify the ESXi node.
That requires a small change to the code (comment out the Pool line and add the Host line).

#$vmSpec.Location.Pool = (Get-ResourcePool -VM $VM).ExtensionData.MoRef
$vmSpec.Location.Host = $vm.ExtensionData.RunTime.Host

 


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

View solution in original post

8 Replies
LucD
Leadership
Leadership
Jump to solution

That thread was using the Windows Task Scheduler to clone a VM, but you can also use a vCenter Scheduled Task with the MethodAction.

See for example Re: Creating a scheduled task to clone a VM - VMware Technology Network VMTN
But you will have to specify the name of the cloned VM (argument 2).
You can't have the same name!

Out of curiosity, why are you worried about Get-Random?


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

0 Kudos
casaub
Enthusiast
Enthusiast
Jump to solution

As I understand 'Get-Random', vCenter will use a random ESXi host of the cluster. But this is not what we need. We would need to have the cloned VM on the ESXi host where the VM is running.

 

Correct, the second clone cannnot have the same name as the first one. That's why your script was a perfect solution because it gave every clone a name based on the date - which makes the name unique.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The script you pointed to does need to run outside the VCSA, it uses the Windows Task Scheduler.
You can use the same ESXi node, just change the selection of the ESXi node.

If you use the VCSA Task Scheduler, you have to provide the new name of the VM when creating the Scheduled Task.
So no date or time in there I'm afraid.


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

0 Kudos
casaub
Enthusiast
Enthusiast
Jump to solution

When the scheduled task for a VM clone in vCSA was created, a new/different name was provided for the clone, yes. VM's name is different from the VM's clone name (like: VM name = productionVM, VM clone name: productionVM_clone).

 

But what happens when this scheduled task runs for the second time. Because it will find the first clone (on same ESXi host, in same folder, same VMFS storage, named 'productionVM_clone') and will try to create a new clone, also named 'productionVM_clone', isn't it?

 

To avoid that name issue I thought your script could be the solution...

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Yes, that is correct.

With the VCSA Scheduled Task the name of the cloned VM is fixed, once the scheduled task is created.
You could update the Scheduled Task each time it has run, but that again requires a box to run that update script.

With the other script, that uses the Windows Task Scheduler, the cloned VM's name can be adapted each time the script runs.
The drawback, you need a Windows box to use the Windows Task Scheduler.

You can't run the second script on the VCSA, since it requires that Windows Task Scheduler.

On top of that, I would strongly advise against running anything on the VCSA.
It's a matter of reliability and performance of the VCSA.
Not to mention the support question.
Plus after each update of the VCSA, you will need to reinstall everything.


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

casaub
Enthusiast
Enthusiast
Jump to solution

> With the VCSA Scheduled Task the name of the cloned VM is fixed, once the scheduled task is created.
> You could update the Scheduled Task each time it has run, but that again requires a box to run that update script.

Unfortunately not very user friendly and hard to understand. Because why is it possible to schedule a task in vCenter for cloning a VM (also hourly) if it is clear that the second run will fail anyway (due to the naming problem).

 

We will have a closer look at the script for the Windows task manager. That this specifies a new name for the clone is exactly what vCSA should be capable of as well.

 

Thank you for the advice reg. vCSA.

 

One last question reg. your script and specifying the ESXi host. When removing 'Get-Random', will the script use the ESXi host where the VM is running on?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I'm afraid that is a shortcoming of how Scheduled Tasks in the VCSA work.
It would be the same if you create the Scheduled Task via the Web Client.
The API that is used by the Clone_VM method expects a name, unfortunately you can't use variables in that name.
So it will always be the same.

When I create a clone every day, I first have another Scheduled Task that removes the clone of the previous day.
Then the same name is no issue.

The sample script uses the Pool to define where the clone should be located.
But you can also specify the ESXi node.
That requires a small change to the code (comment out the Pool line and add the Host line).

#$vmSpec.Location.Pool = (Get-ResourcePool -VM $VM).ExtensionData.MoRef
$vmSpec.Location.Host = $vm.ExtensionData.RunTime.Host

 


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

casaub
Enthusiast
Enthusiast
Jump to solution

We will give this a try. Thank you very much.

0 Kudos