VMware Cloud Community
Tirou
Contributor
Contributor

New-Template cmdlet to create templates across data stores?

Hi

I have a need to copy templates across datastores. We have about 20 datastores where I need to replicate my windows templates in each of the 20 datastores. It may look weird since one set of templates is enough for the whole VC environment. But we have some automation built that requires the template should be present in each datastores in the VC.

I thought of using New-template cmdlet to replicate the templates. But the cmdlet does not have the option to choose which datastore teh template should get created (We have multiple datastores in our clusters and I prefer the templates should get created on a particular datastore).

Anyway to achieve this using new-template cmdlet? If not possible I am looking for a simple script to copy my templates across datastores. Appreciate any help.

Thanks, Tirou

0 Kudos
1 Reply
RvdNieuwendijk
Leadership
Leadership

Hi Tirou,

a PowerCLI template object misses a lot of features that a VM object has. So the easiest thing is to change the template into a VM, clone the VM and change the old and new VM's back into templates. Something like:

$Template = "MyTemplate"
$Datastore = "MyDatastore"

# Change to template to a VM
$vm = Set-Template -Template $Template -ToVM

#Create a list of datastores the template uses
$Datastores = @()
$vm.DatastoreIdList | ForEach-Object {
  $Datastores += (Get-View -Id $_).Name
}

# Only copy the template if the datastore is not already used by the template
if ($Datastores -notcontains $Datastore) {
   $NewVM = New-VM -Name "$Template-$Datastore" -VMHost $vm.VMHost -VM $vm -Datastore $Datastore -Confirm:$false
}

#Change the VM's back to templates
$OldTemplate = Set-VM -VM $vm -ToTemplate
$NewTemplate = Set-VM -VM $NewVM -ToTemplate


Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos