VMware Cloud Community
nirvy
Commander
Commander

Clone a template (e.g. to create another template with diferent name/datastore)

Hi

Is it still the case with PowerCLI 4.0 U1 that in order to copy a template, you first need to convert it to a VM, clone it and then mark source and target vms as templates?

For example:

Function Copy-Template ($template, $newtemplate, $tgthost, $tgtds) {
    $vm = Set-Template -Template (Get-Template $template) -ToVM
    $cpvm = New-VM -Name $newtemplate -VM $vm -Datastore (Get-Datastore -name $tgtds) -VMHost $tgthost
    (get-vm $cpvm | get-view).MarkAsTemplate() | Out-Null
    (get-vm $vm | get-view).MarkAsTemplate() | Out-Null
}

Does anybody know of a better way?

0 Kudos
4 Replies
nirvy
Commander
Commander

Update:

Not sure why I thought I needed to convert the source template to a VM first, must of had a blonde moment.

New function is:

Function Copy-Template ($template, $newtemplate, $tgthost, $tgtds) {
    $vm = New-VM -Name $newtemplate -Template $template -Datastore (Get-Datastore -name $tgtds) -VMHost $tgthost
    (get-vm $vm | get-view).MarkAsTemplate() | Out-Null
}

Is this the best way to do it? or is there a powercli cmd-let I missed?

0 Kudos
LucD
Leadership
Leadership

The Set-VM and Set-Template cmdlets have new parameters, respectively -ToTemplate and -ToVM, which allow you to convert from template to vm and back.

So you could do something like

New-Template -VM (Get-Template <src-template> | Set-Template -ToVM) -Name <dst-template> ...

____________

Blog: LucD notes

Twitter: lucd22


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

nirvy
Commander
Commander

Cheers Luc, I think I prefer my function though (second post, not first!) as it doesn't need to convert the source template to VM + I can choose a datastore/host for the new template!

Edit: I say "my function"... I probably stole it from your blog anyway Smiley Wink

0 Kudos
LucD
Leadership
Leadership

I agree with you, the New-Template cmdlet should also have a -Template parameter (besides the -VM parameter).

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos