
Storage vMotion is a great feature to Move your VMs to other datastores. But what if you want to move your Templates?
In the current version of vSphere there is no option within the Client:

So I created a PowerCLI function just to migrate the templates to another datastore.
function Move-Template{
param( [string] $template, [string] $esx, [string] $datastore)
if($template -eq ""){Write-Host "Enter a Template name"}
if($esx -eq ""){Write-Host "Enter an ESX hostname"}
if($esx -ne "" -and $datastore -eq ""){$vmotion = $true}
if($datastore -ne ""){$svmotion = $true}
Write-Host "Converting $template to VM"
$vm = Set-Template -Template (Get-Template $template) -ToVM
if($svmotion){
Write-Host "Migrate $template to $esx and $datastore"
Move-VM -VM (Get-VM $vm) -Destination (Get-VMHost $esx) `
-Datastore (Get-Datastore $datastore) -Confirm:$false
(Get-VM $vm | Get-View).MarkAsTemplate() | Out-Null
}
if($vmotion){
Write-Host "Migrate $template to $esx"
Move-VM -VM $vm -Destination (Get-VMHost $esx) -Confirm:$false
($vm | Get-View).MarkAsTemplate() | Out-Null
}
}
The function above can be used to move a single template via
Move-Template <template> <esxhost> <datastore>
But what if you want to move only your Linux Templates or Windows Templates or even all the Templates at once. For these options, I created two extra functions.
First I created a function to get al the Linux templates:
function Get-LinuxTemplates{
$lnxtpl = Get-Template | Get-View | `
where {$_.Guest.GuestFamily -eq 'linuxGuest'} | `
Get-VIObjectByVIView
return $lnxtpl
}

And if you want, you can also get al the Windows templates:
function Get-WindowsTemplates{
$wintpl = Get-Template | Get-View | `
where {$_.Guest.GuestFamily -eq 'windowsGuest'} | `
Get-VIObjectByVIView
return $wintpl
}

Now we can get all the different templates, we are able to move the templates to another host or datastore.
This is how you move all the templates to a new host and datastore:
$templates = Get-Template
foreach($tpl in $templates){
Move-Template $tpl <esxhost> <datastore>
}

If you want to move all the Linux templates, you run the following commands:
$templates = Get-LinuxTemplates
foreach($tpl in $templates){
Move-Template $tpl <esxhost> <datastore>
}
And finally, you can move all windows templates by running these commands:
$templates = Get-WindowsTemplates
foreach($tpl in $templates){
Move-Template $tpl <esxhost> <datastore>
}
But how does it look when you run the function. I will do an example with the get-linuxtemplates function.

I am going to use this functions in another script and will transform it to a ready to use script for the EcoShell.