VMware Cloud Community
rbutter
Contributor
Contributor
Jump to solution

Clone Template to template

I need a simple PowerCli script to clone an ESXi 4.1 template on one datastore to a template on another datastore.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Bad luck, the PowerCLI 4.1 New-Template cmdlet didn't have the Template parameter yet.

An alternative is to temporarily convert the source template to a VM (with the Set-Template cmdlet), then do the New-Template with the VM parameter and when completed convert the VM back to a template (with the Set-VM cmdlet and the ToTemplate switch).

Some extra steps but that should work in PowerCLI 4.1


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

View solution in original post

0 Kudos
17 Replies
vmroyale
Immortal
Immortal
Jump to solution

Note: Discussion successfully moved from VMware ESXi™ 4 to VMware vSphere™ PowerCLI

Brian Atkinson | vExpert | VMTN Moderator | Author of "VCP5-DCV VMware Certified Professional-Data Center Virtualization on vSphere 5.5 Study Guide: VCP-550" | @vmroyale | http://vmroyale.com
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can clone a template and pass the target datastore.

Something like this

$tgtDatastore = "DS2"
New-Template -Name NewTemplate -Template OldTemplate -Datastore $tgtDatastore


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

0 Kudos
rbutter
Contributor
Contributor
Jump to solution

Luc, I get an error that says "A parameter cannot be found that matches Parameter Name Template"

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You did replace my sample name "OldTemplate" with a name of one your templates ?
How does your line look ?

Or you could be running an older PowerCLI version ?

Which version do you have ? Do a

Get-PowerCLIVersion


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

0 Kudos
rbutter
Contributor
Contributor
Jump to solution

$tgtDatastore="ISCSiDatastore"

$newT="RHEL6GDc

$oldT="RHEL6GD"

New-Template -Name $newT -Template $oldT -Datastore $tgtDatastore -Confirm:$false

0 Kudos
rbutter
Contributor
Contributor
Jump to solution

I'm running version 4.1 Update 1 and unfortunately cannot upgrade because I don't own the system.

Thanks for your assistance!

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Bad luck, the PowerCLI 4.1 New-Template cmdlet didn't have the Template parameter yet.

An alternative is to temporarily convert the source template to a VM (with the Set-Template cmdlet), then do the New-Template with the VM parameter and when completed convert the VM back to a template (with the Set-VM cmdlet and the ToTemplate switch).

Some extra steps but that should work in PowerCLI 4.1


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

0 Kudos
rbutter
Contributor
Contributor
Jump to solution

Thanks Luc, I don't want to do that for several reasons so I'll have to try and talk the system owners into upgrading the PowerCli to the latest version.

0 Kudos
Tocano
Enthusiast
Enthusiast
Jump to solution

Can you not deploy a new VM from the template, migrate that VM to the new datastore, then convert that new VM back into a template?

Something like:

> $newVM = Get-Template "template-name" | New-VM

> $newVM | Move-VM -Datastore (Get-Datastore "destination-datastore-name")

> $newVM | New-Template

Just a thought.

0 Kudos
rbutter
Contributor
Contributor
Jump to solution

Tocano, yes I believe I could do that however, I want to avoid converting the template to a VM and save as much time as possible in the cloning process
mainly because I have about 55 templates to clone to a different datastore and the process may be done frequently.  Thanks

0 Kudos
Tocano
Enthusiast
Enthusiast
Jump to solution

Might also take a look at the Copy-DatastoreItem cmdlet. Not sure if that would work for what you're wanting, but might be an option to try. Good luck.

0 Kudos
rbutter
Contributor
Contributor
Jump to solution

Is it possible to keep the original VM date and time stamp when you clone it?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You mean the date and time on the files that constitute the VM ?


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

0 Kudos
rbutter
Contributor
Contributor
Jump to solution

Yes, so if i look at the Vm files Modified On info in the datastore browser of the clone they match the original

0 Kudos
Tocano
Enthusiast
Enthusiast
Jump to solution

I don't believe that happens even if you use New-Template. These are basically Linux filesystems. So a new file get a new modified date.

Yep, just tested with PowerCLI 5.5 using New-Template and the new copy gets new Modified dates.

0 Kudos
rbutter
Contributor
Contributor
Jump to solution

I was thinking there may be a switch you could set in the PowerCLI script to preserve the date\time stamp similar to a DD copy.

0 Kudos
rbutter
Contributor
Contributor
Jump to solution

Luc, is there such a switch to set in powercli when cloning templates that will retain the original creation dates?  Also, I am running your script to add VMs to vCenter Inventory. It works fine on a few but, on the majority it is registering the vmdk file instead of the vmx.  So instead of the VM name when registered I get the vmdk id string.  Any thoughts?

0 Kudos