Automation

 View Only
  • 1.  Copy tags from 1 vcenter to another

    Posted May 20, 2022 10:46 AM

    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"}
    }
    }


  • 2.  RE: Copy tags from 1 vcenter to another
    Best Answer

    Posted May 20, 2022 10:54 AM

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



  • 3.  RE: Copy tags from 1 vcenter to another

    Posted May 31, 2022 02:14 PM

    Thanks, exactly what i needed 



  • 4.  RE: Copy tags from 1 vcenter to another

    Posted May 27, 2022 06:55 PM

    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



  • 5.  RE: Copy tags from 1 vcenter to another

    Posted May 28, 2022 05:45 PM

    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