VMware Cloud Community
xpurpleblack
Contributor
Contributor

Clone Templates to Specific Folder with Specific Name

Hey Everyone,

Brand new to PowerCLI and had a task I would like some help with

I need to clone a specific group of templates to a new folder.

The templates that need to be cloned all start with "VRA"-(name of template)

I would like the clones to have "Dev-VRA" (Name Of template) and a new folder that is already created (I can get the folder name/ID)

Thanks for the help!

Reply
0 Kudos
8 Replies
LucD
Leadership
Leadership

You could do something like this

$srcName = 'VRA*'

$folderName = 'NewFolder'


$folder = Get-Folder -Name $folderName

Get-Template -Name $srcName |

ForEach-Object -Process {

   New-Template -Name "Dev-$($_.Name)" -Template $_ -Location $folder

}


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

xpurpleblack
Contributor
Contributor

LucD the Forum Sensei,

I realized we also use one datastore and one compute resource for these templates, how can I make sure they are in the correct one in our cluster?

Thank you for the help I am going to give this a shot

Reply
0 Kudos
LucD
Leadership
Leadership

You can explicitly specify the same ESXi and the same datastore.

Something like this

$srcName = 'VRA*'

$folderName = 'NewFolder'


$folder = Get-Folder -Name $folderName

Get-Template -Name $srcName |

ForEach-Object -Process {

   $esx = Get-VIObjectByVIView -MORef $_.HostId

   $ds = Get-VIObjectByVIView -MoRef $_.DatastoreIdList[0]

   New-Template -Name "Dev-$($_.Name)" -Template $_ -Location $folder -VMHost $esx -Datastore $ds

}


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

xpurpleblack
Contributor
Contributor

Thank you!

One last thing can you also add context to each line?

Reply
0 Kudos
LucD
Leadership
Leadership

Sure

$srcName = 'VRA*'

$folderName = 'NewFolder'

$folder = Get-Folder -Name $folderName

Get-Template -Name $srcName |

ForEach-Object -Process {

   $esx = Get-VIObjectByVIView -MORef $_.HostId

   $ds = Get-VIObjectByVIView -MoRef $_.DatastoreIdList[0]

   New-Template -Name "Dev-$($_.Name)" -Template $_ -Location $folder -VMHost $esx -Datastore $ds

}

Annotations

Line 1-2: setting the values we are using. The $srcName uses the meta-character '*' to specify that we don't care what comes after 'VRA'

Line 5: getting the target folder for all clones

Line 6-7: we get all templates whose name starts with 'VRA'

Line 8-9: we get the current ESXi node and datastore on which the template is located. This to avoid that one of these might change during the clone process (possibly due DRS or SDRS)

Line 10: the actual cloning of the template, during which we compose the name of the new template, based on the name of the old template with a prefix 'Dev-'


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

Reply
0 Kudos
xpurpleblack
Contributor
Contributor

Thank you Luc!

Reply
0 Kudos
JB1024
Contributor
Contributor

Is it possible to use a script like this to clone all templates to three different vcenters in different SSO domains?
Reply
0 Kudos
LucD
Leadership
Leadership

That should be possible by using the Server parameter on the cmdlets, and looping through all the vCenters.


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

Reply
0 Kudos