Hi LucD,
I'm now revisiting this same script but extending it to add another 2 vCenters (for production).
The previous one was for (non-production).
Whats the best way of add 2 new vCenters?
The current script moves all the non-production templates from one vCenter to another.
#Import PowerCLI module
Import-module vmware.vimautomation.core
#Variables
$vc1 = 'vcenter1'
$vc2 = 'vcenter2'
$credfile = 'C:\Users\ACCOUNT\AppData\Roaming\VMware\credstore\vicredentials.xml'
$cred = Get-VICredentialStoreItem -File $credfile -Host $vc2
#Connect to vCenters
Connect-VIServer -Server $vc1
Connect-VIServer -Server $vc2 -User $cred.User -Password $cred.Password
#Import CSV file with template names
$currentTemplates = Import-csv \\SERVER\Templates.csv
foreach($template in $currentTemplates){
#Check if all templates exist in vCenter2. If found, delete template permanently (inventory and files). If not found (error action), go to Catch.
Try{
$checkTemplates = Get-Template -Name $($template.template) -Server $vc2 -ErrorAction Stop
Remove-Template -Template $checkTemplates -Server $vc2 -DeletePermanently -Confirm:$false
Write-Output "Removed template $($template.template) on vCenter $vc2"
}
Catch{
Write-Output "Template $($template.template) not found on vCenter $vc2"
}
# Get destination host and datastore
$esx2 = Get-VMHost -Name $template.vmhost -Server $vc2
$ds2 = Get-Datastore -Name $template.datastore -Server $vc2
#Clone existing templates in vCenter1 to vCenter1 in the same folder with a "-clone" amended to the name.
$newTemplate = "$($template.template)-clone"
New-Template -Template $($template.template) -Name $newTemplate -Confirm:$false
Write-Output "Cloned template $($template.template) to $newTemplate on vCenter $vc1"
#Convert the cloned template to VM
Write-Output "Converting template $newTemplate to a VM on vCenter $vc1....."
Set-Template -Template $newTemplate -ToVM -Confirm:$false -Server $vc1
#Remove the NIC from VM and migrate to vCenter2
Write-Output "Migrating VM $newTemplate to vCenter $vc2 on host $esx2 and datastore $ds2..."
$nic = Get-NetworkAdapter -VM $newTemplate
Remove-NetworkAdapter -NetworkAdapter $nic -Confirm:$false
#vMotion VM from vc1 to vc2 on specific host and datastore
Move-VM -VM $newTemplate -Destination $esx2 -Datastore $ds2 -Server $vc1
#Add NIC with specific port group
New-NetworkAdapter -VM $newTemplate -NetworkName "vCenter2 port group name" -StartConnected -Type Vmxnet3
#Rename VM to original name
Write-Output "Renaming VM $newTemplate to $($template.template) on vCenter $vc2...."
$newVM = Set-VM -VM $newTemplate -Name $($template.template) -Confirm:$false -Server $vc2
#Move VM into a specific folder
Move-VM -VM $newVM -Destination "Templates" -Server $vc2
#Convert VM to template
Write-Output "Converting VM $newVM to a template on vCenter $vc2....."
Set-VM -VM $newVM -ToTemplate -Confirm:$false -Server $vc2
}
#Disconnect from both vc's
Disconnect-VIServer -Server $vc1 -Confirm:$false
Disconnect-VIServer -Server $vc2 -Confirm:$false