6 Replies Latest reply on Mar 11, 2019 11:09 AM by Jrt1general

    Importing Tags using PowerCLI on vCenter 6.7 fails to assign tags to VMs

    Jrt1general Novice

      I've been trying to use the code in the original thread I started a few months ago. It's been exceptionally helpful in demonstrating tag performance issues with our TAM and GSS case. As we move towards 6.7 I want to replicate the same tests and assign a huge batch of tags and categories to our test environment to verify we're going to be safe rolling out tags into production. However it seems like something in script is broken when interacting with 6.7. It works find (same script, same powerCLI/powershell, etc) when run against 6.0

       

      Testing in vCenter 6.0 and succeeded in running this script: https://code.vmware.com/forums/2530#598690|4398020

       

      Copied below to save a click (sorry about formatting - how do YOU make your code look good here?)

       

      $CMDBInfo = Import-CSV .\cmdbinfo.csv
      # Get the header names to use as tag category names
      $TagCatNames = $cmdbinfo | Get-Member | Where-Object {$_.MemberType -eq "NoteProperty"} | Select-Object -Expand Name
      # Create the Tag Category if it doesnt exist
      Foreach ($Name in ($TagCatNames | Where-Object {$_ -ne "Name"})) {
        Try {
         $tCat = Get-TagCategory $Name -ErrorAction Stop
        }
        Catch {

       

         Write-Host "Creating Tag Category $Name"
         $tCat = New-TagCategory -Name $Name # -Description "$Name from CMDB"
        }
        # Create Tags under the Tag Categories
        $UniqueTags = $cmdbinfo | Select-Object -expand $Name | Get-Unique
        Foreach ($Tag in $UniqueTags) {
         Try {
         $tTag = Get-Tag $Tag -Category $tCat -ErrorAction Stop
         }
         Catch {
         Write-Host "..Creating Tag under $Name of $Tag"
         $tTag = New-Tag -Name $Tag -Category $tCat # -Description "$Tag from CMDB"
         }

       

         # Assign the Tags to the VMs/Hosts
         $cmdbinfo | Where-Object {$_.($Name) -eq $Tag} | Foreach-Object {
         Write-Host ".... Assigning $Tag in Category of $Name to $($_.Name)"
         New-TagAssignment -Entity $($_.Name) -Tag $tTag | Out-Null
         }
        }
      }

       

      I went back and verified in our test lab vCenter 6.0 (latest update) that this script worked flawless.

       

      However in vCenter 6.7 the script fails to actually assign the tag to the VM. It is able to populate tags and categories without issue but when it comes time to assign a tag to the VM,

       

      New-TagAssignment : 2/6/2019 10:16:42 AM        New-TagAssignment              Value cannot be null.

       

      Parameter name: collection

       

      At C:\Users\redacted\Desktop\Tagging Project\Import_tag.ps1:37 char:5

       

      +     New-TagAssignment -Entity $($_.Name) -Tag $Tag

       

      +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

       

          + CategoryInfo          : NotSpecified: (:) [New-TagAssignment], VimException

       

       

          + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.NewTagAssignment

       


      If I nuke the categories and tags in 6.7, and manually set them up:

       

      PS C:\Users\redacted\Desktop\Tagging Project> New-TagCategory Application -Cardinality multiple

       

       

       

      Name                                     Cardinality Description

       

      ----                                     ----------- -----------

       

      Application                              Multiple

       

       

       

      PS C:\Users\redacted\Desktop\Tagging Project> New-tag TestApp1 -category Application

       

       

       

      Name                           Category                       Description

       

      ----                           --------                       -----------

       

      TestApp1                       Application

       

       

       

      PS C:\Users\redacted\Desktop\Tagging Project> New-TagAssignment -entity TagTestVM_00 -Tag TestApp1

       

      New-TagAssignment : 2/6/2019 11:17:20 AM        New-TagAssignment              Value cannot be null.

       

      Parameter name: collection

       

      At line:1 char:1

       

      + New-TagAssignment -entity TagTestVM_00 -Tag TestApp1

       

      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

       

          + CategoryInfo          : NotSpecified: (:) [New-TagAssignment], VimException

       

       

       

          + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.NewTagAssignment

       

       

       

      Even just breaking it out further:

       

      PS C:\Users\redacted\Desktop\Tagging Project> New-TagAssignment -Tag TestApp1 -Entity TagTestVM_00

       

      New-TagAssignment : 2/6/2019 11:21:34 AM        New-TagAssignment              Value cannot be null.

       

      Parameter name: collection

       

      At line:1 char:1

       

      + New-TagAssignment -Tag TestApp1 -Entity TagTestVM_00

       

      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

       

          + CategoryInfo          : NotSpecified: (:) [New-TagAssignment], VimException

       

       

       

          + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.NewTagAssignment

       

       

       

      And finally from the basic command:

       

      PS C:\Users\redacted\Desktop\Tagging Project> New-TagAssignment

       

      cmdlet New-TagAssignment at command pipeline position 1

       

      Supply values for the following parameters:

       

      Tag: TestApp1

       

       

       

      Cannot convert the "TestApp1" value of type "System.String" to type "VMware.VimAutomation.ViCore.Types.V1.Tagging.Tag".

       

       

       

      New-TagAssignment : 2/6/2019 10:16:42 AM        New-TagAssignment              Value cannot be null.

       

      Parameter name: collection

       

      At C:\Users\j129244_su\Desktop\Tagging Project\Import_tag.ps1:37 char:5

       

      +     New-TagAssignment -Entity $($_.Name) -Tag $Tag

       

      +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

       

          + CategoryInfo          : NotSpecified: (:) [New-TagAssignment], VimException

       

          + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.NewTagAssignment