PowerCLI

 View Only
Expand all | Collapse all

Looking for Empty Tags

  • 1.  Looking for Empty Tags

    Posted Aug 23, 2019 01:06 PM

    Note: powershell newbie!

    I am looking for help with a powershell script that will find all tags that are empty.  I have a script that will give me a list of servers are the tags applied to them already.  But the script does not identify tags that do not have any VMs associated with them. 

    Does anyone have the right commands to identify these, so I can work to keep them cleaned up periodically?  Thanks!



  • 2.  RE: Looking for Empty Tags

    Posted Aug 23, 2019 01:20 PM

    You could use something like this.

    It uses a hash table ($tagTab) where the script first creates entries with a key composed of Category/Tag.
    Then it lists all Tag assignments and removes the Category/Tag entry from the hash table.

    What is left, should be the tags that are not used.

    $tagTab = @{}

    Get-Tag | %{

      $tagTab.Add("$($_.Category)/$($_.Name)",'')

    }

    Get-TagAssignment | %{

      $key = "$($_.Tag.Category.Name)/$($_.Tag.Name)"

       if($tagTab.ContainsKey($key)){

      $tagTab.Remove($key)

      }

    }

    Write-Host "Unused Category/Tag`n"

    $tagTab.Keys



  • 3.  RE: Looking for Empty Tags

    Posted Aug 23, 2019 02:07 PM

    Unfortunately, that didn't work.  I am connected to two different vcenters.  Here is the error when I run it while connected to both (continuous errors until "cntl-c" - no results):

    Exception calling "Add" with "2" argument(s): "Item has already been added. Key in dictionary: 'AppCode/CO2710'  Key

    being added: 'AppCode/CO2710'"

    At C:\temp\emptytag.ps1:4 char:33

    +   $tagTab.Add("$($_.Category)/$($_.Name)",'')

    +                                 ~~~~~~~

        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

        + FullyQualifiedErrorId : ArgumentException

    When I run the script while only connected to one vcenter, I get results returned, however it appears to be all tags (regardless of empty or not).  And I receive a different error:

    Get-TagAssignment : 8/23/2019 9:59:52 AM        Get-TagAssignment               com.vmware.vapi.std.errors.internal_server_error

    {'messages': [com.vmware.vapi.std.localizable_message {'id': vapi.bindings.method.impl.unexpected, 'default_message':

    Provider method implementation threw unexpected exception: com.vmware.vapi.std.errors.InternalServerError, 'args':

    [com.vmware.vapi.std.errors.InternalServerError]}, com.vmware.vapi.std.localizable_message {'id':

    vapi.bindings.method.impl.unexpected, 'default_message': Provider method implementation threw unexpected exception:

    null, 'args': [null]}], 'data':}

    At C:\temp\emptytag.ps1:8 char:1

    + Get-TagAssignment | %{

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

        + CategoryInfo          : NotSpecified: (:) [Get-TagAssignment], CisException

        + FullyQualifiedErrorId : VMware.VimAutomation.ViCore.Impl.V1.Service.Tagging.Cis.TaggingServiceCisImpl.GetTagAssi

       gnment.Error,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.GetTagAssignment



  • 4.  RE: Looking for Empty Tags

    Posted Aug 23, 2019 02:20 PM

    The 2 vCenters is normal with the current logic in the script.
    You have the same Category/Tag on both vCenters, which explains the duplicate key.

    If it is intended to be used over multiple vCenters, I think the vCenter should be part of the key.

    That Get-TagAssignment exception was a known issue in previous PowerCLI releases.

    Which PowerCLI version are you using?



  • 5.  RE: Looking for Empty Tags

    Posted Aug 23, 2019 02:27 PM

    Name                           Value

    ----                           -----

    PSVersion                      5.1.17134.858

    PSEdition                      Desktop

    PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}

    BuildVersion                   10.0.17134.858

    CLRVersion                     4.0.30319.42000

    WSManStackVersion              3.0

    PSRemotingProtocolVersion      2.3

    SerializationVersion           1.1.0.1



  • 6.  RE: Looking for Empty Tags

    Posted Aug 23, 2019 02:48 PM

    I mean your PowerCLI version.
    Do a

    Get-Module -Name VMware.PowerCLI -ListAvailable



  • 7.  RE: Looking for Empty Tags

    Posted Aug 23, 2019 03:00 PM

    ModuleType Version    Name                                ExportedCommands

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

    Manifest   11.0.0.... VMware.PowerCLI



  • 8.  RE: Looking for Empty Tags

    Posted Aug 23, 2019 03:02 PM

    We are currently at 11.4.0, and there have been several improvements on the Tag cmdlets.
    Can you try to upgrade the PowerCLI module?



  • 9.  RE: Looking for Empty Tags

    Posted Aug 23, 2019 03:28 PM

    Done!

    ModuleType Version    Name                                ExportedCommands

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

    Manifest   11.4.0.... VMware.PowerCLI

    Manifest   11.0.0.... VMware.PowerCLI

    I attempted the script again (while connected to one and both vcenters) and I received the exact same results as before.



  • 10.  RE: Looking for Empty Tags

    Posted Aug 23, 2019 03:38 PM

    Ok, the 2 vCenter error is normal as I explained before.
    The script currently doesn't handle duplicate keys.

    Just to make sure, you did stop/start your PowerShell/PowerCLI session after upgrading the PowerCLI module?



  • 11.  RE: Looking for Empty Tags

    Posted Aug 23, 2019 04:43 PM

    I actually closed all windows and re-connected to the vcenters.

    Thank you for your help anyway!



  • 12.  RE: Looking for Empty Tags

    Posted Aug 23, 2019 04:59 PM

    That 2nd error seems to be a problem with cmdlet and/or your environment.

    I would suggest opening an SR for that.

    You could also give it a try with the cmdlets from the VMware.Community.CISTag module.
    See New Community Module for Tag Management



  • 13.  RE: Looking for Empty Tags

    Posted Aug 23, 2019 05:10 PM

    Thanks LucD!  I'll look into the new module as well.  I appreciate your assistance!!