VMware Cloud Community
VirtPhysical
Enthusiast
Enthusiast
Jump to solution

Copy tags from 1 vcenter to another

Im trying to copy all Tags from 1 vCentre to another, while my script works it also throws up a lot of errors when checking if the tag category & tags already exist. Can anyone suggest where I'm going wrong? I assume I'm using | out-null incorrectly? 

Function CreateCategories { 
$LocalVC = "localvc"
$RemoteVC = "remotevc"
$localCategories = Get-TagCategory -server $LocalVC
ForEach ($Category in $localCategories)
{
If ((get-tagcategory $Category.Name -server $RemoteVC | Out-Null) -eq $null) {New-TagCategory -Name $Category.Name -server $RemoteVC} else { Write-Output "Category $Category.Name Already Exists"}
$localtags = Get-TagCategory $Category.Name -server $LocalVC | Get-Tag
ForEach ($tag in $localtags)
{
If ((Get-TagCategory -Name $Category.Name -server $RemoteVC | get-Tag -Name $tag.Name | Out-Null) -eq $null) {Get-TagCategory -Name $Category.Name -server $RemoteVC | New-Tag -Name $tag.Name} else { Write-Output "Tag $tag.Name already exists"}
}
}
Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Did you try adding -ErrorAction SilentlyContinue on the cmdlets that give errors?


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

View solution in original post

4 Replies
LucD
Leadership
Leadership
Jump to solution

Did you try adding -ErrorAction SilentlyContinue on the cmdlets that give errors?


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

papam2
Contributor
Contributor
Jump to solution

Run Disconnect-VIServer -Server * -Force before running and after running on each vCenter. I had a similar issue where PowerCLI didn't understand that it should run in another vCenter. Even $defaultviserver indicating vCenter X, get-tag listed was returning tags on vCenter y

Reply
0 Kudos
MaxB
Enthusiast
Enthusiast
Jump to solution

Hi,

not sure but I would try to compare the local categories with the remote categoriere

$localCategories = Get-TagCategory -server $LocalVC
$remoteCategories = Get-TagCategory -server $RemoteVC

ForEach($Category in $localCategories.Name) 
{
If ($remoteCategories.Name -notcontains $Category.Name)
{
Write-Output "yeah so I should create it..."
}
else 
{
Write-Output "seems to exist nothing to to here..."
}
}


If it helps I would appreciate a kudo 🙂

 

VirtPhysical
Enthusiast
Enthusiast
Jump to solution

Thanks, exactly what i needed 

Reply
0 Kudos