Thursday, May 19, 2022

vSphere Tags after Cross vCenter vMotion

We will explain here how you can reapply the vSphere Tags after you migrate a VM from one vCenter to another or even when you use VEEAM or any other replication tool to replicate a VM from one vCenter to another vCenter


Before executing this script you will need to create 2 folders in the 2 vCenters with the same name. and put the machines under these folders. because the main Idea of the script is getting the VMs under a certain folder and collect their TAGs , and then login to the other vCenter and search for the collected VMs and start to apply the TAGs on them. you also will need to make sure that the TAGs exist on the destination VC (The script will not create the TAGs , it will apply them only)


Import-Module VMware.VimAutomation.Core
 
$vCenterIP = "First vCenter"
$FOLDERNAME = 'XXXX'          ##Change XXXX with folder Namer###
Connect-VIServer $vCenterIP

$tagsall = @()

foreach($machine in $machines = Get-Folder -Name $FOLDERNAME | Get-VM){
$tagsall+=@(Get-TagAssignment -Entity $machine.name)
}
$tagsall

sleep 10

Disconnect-VIServer $vCenterIP -confirm:$false


1. Don't close your Powershell window. 

2. Start to do the cross vCenter vmotion.

3. After you finish from all the VMs.

4. Execute the below script.


$vCenterIP = "Second vCenter"
Connect-VIServer $vCenterIP -User $vCenterUser -Password $vCenterPass 

$tagsallnew = $tagsall | select Tag,Entity

for($i = 0; $i -lt $tagsallnew.length; $i++){

 $tag = Get-Tag -name $tagsallnew[$i].tag.name -Category $tagsallnew[$i].tag.Category.name   -Server "Second vCenter"
 $vm = get-vm -name $tagsallnew[$i].Entity.Name   -Server "Second vCenter"

 New-TagAssignment -Tag $tag  -Entity $vm -Server "Second vCenter"
 }



No comments:

Post a Comment

NSX-T (local log in) with VIDM integration

We will explain how to log in with a local account to your NSX-T which integrated with VIDM. We integrated our NSX-T into the VIDM. We had a...