Home > Blogs > VMware PowerCLI Blog


Using Tags with PowerCLI

The Tagging feature was introduced in vCenter Server 5.1 where custom defined tags can be categorized and added to any of your inventory objects in your environment.

Tags can be quickly searched on and retrieved quickly. Multiple unique tags can be used on the same inventory object creating granular metadata that can easily be grouped and search on.

As an example this is helpful for looking for all VMs tagged against a particular project or placing all departmental objects into groups, IE HR requiring not just there VMs but networking and datastores used as well, This all benefits the administrator by simplifying management and making information more easily available.

 

In PowerCLI 5.5 R1 we introduced some cmdlets for working with tag assignments and easily allowing multiple inventory objects to be tagged or removed from a tag in an automated fashion.

Tag Cmdlets

An initial discovery shows 4 new tag cmdlets to work with inventory objects, but don’t forget that you can also use some of the existing cmdlets with tag information as well, such as Get-VM –Tag “Gold”.

SNAGHTMLfa9d8b

Get-VM/VMHost/Datastore/VirtualPortGroup/VDPortGroup

These cmdlets now have tag parameter

The following example returns all VMs with a tag of “Gold”.

Get-VM –Tag Gold

The following example returns all VMs with a tag of “Gold”.

Get-Tag

The following example returns all tags named “MyTag”.

Get-Tag -Name MyTag

The following example returns all tags from the “MyCategory1″ and “MyCategory2″ categories, named “MyTag”.

Get-Tag -Category MyCategory1, MyCategory2 -Name MyTag

Get-TagAssignment

The following example retrieves all tag assignments for the $datastore entity that have tags from the “MyCategory” category.

$myDatastore = Get-DataStore MyDatastore

Get-TagAssignment -Entity $datastore -Category MyCategory

New-TagAssignment

The following example can be used to assign the “MyTag” tag to all virtual machines whose name contains the “*myvm*” wildcard pattern.

$myTag = Get-Tag MyTag

$myVM = Get-VM ‘*myvm*’

New-TagAssignment -Tag $myTag -Entity $myVM

Remove-TagAssignment

The following example removes all connections to tags from the specified virtual machine entity.

$myVM = Get-VM myvm

$myTagAssignment = Get-TagAssignment $myVM

Remove-TagAssignment $myTagAssignment

See them in action

In the below video we show how to use these cmdlets to easily tag some virtual machines and retrieve tag information for a number of virtual machines with PowerCLI.

 

This entry was posted in Reporting, Tags and tagged , on by .
Alan Renouf

About Alan Renouf

Alan Renouf is a Product Manager at VMware focusing on Automation Frameworks and CLI, he is responsible for providing the architects and operators of the cloud infrastructure with the toolkits/frameworks and command-line interfaces they require to build a fully automated software-defined datacenter. Alan is a frequent blogger at http://blogs.vmware.com/PowerCLI and has a personal blog at http://virtu-al.net. You can follow Alan on twitter as @alanrenouf.

7 thoughts on “Using Tags with PowerCLI

  1. gert

    Hi alan,

    nice stuff.

    question just to clarify:
    Are tags are not stored in the vCenter Database. What is the best way to back them up??

    Gert

    Reply
  2. Amit

    New-TagAssignment : Cannot convert ‘System.Object[]‘ to the type ‘VMware.VimAutomation.Sdk.Types.V1.VIObjectCore’ required by param
    eter ‘Entity’. Specified method is not supported.
    + New-TagAssignment -Tag $myTag -Entity <<<< $myVM
    + CategoryInfo : InvalidArgument: (:) [New-TagAssignment], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.NewTagAssignment

    when trying to execute New-TagAssignment -Tag $myTag -Entity $myVM

    any idea why?

    Reply
  3. Johann Stander

    Hi Alan
    This is very helpful information on the powercli but have been trying to figure out how to provide this information to users from within the vsphere web client.
    When listing all the virtual machines under related objects within a datacenter and adding columns, you can still only add the columns from the old custom attributes which has been migrated to Tags.
    Wonder if you seen anything within web client before to show the tag associations and where assigned.
    Cheers

    Reply
  4. Daryl Greenwood

    When will we see the ability to create, delete, and edit categories and tags using PowerCLI? We have multiple farms with thousands of VMs and we leverage automation wherever possible. One particular custom category I would like to create or modify tags in is called “Application Support Team or Queue”. The data for the tags comes from an external source. We currently populate an attribute with this data, but customers using the WEB client can not see attribute(s). Creating, deleting, and editing categories and tags is currently not possible using PowerCLI. We also have an attribute called “Created On” that shows the VM creation date that just does not seem to fit into tags very well unless I want thousands of them. We are forced to dump all types of information into the notes field for our WEB users, making it difficult to report on and maintain.

    Is there any way to stop the web converter from creating its own description (Besides changing each entry manually)?
    I do not like seeing:
    Migrated from custom attribute:Application Support Team or Queue
    Migrated from custom attribute:Application Support Team or Queue (1)
    Migrated from custom attribute:Application Support Team or Queue (2)
    I want them blank.

    The transition from attributes to tags has not been very graceful. :-(

    Reply
  5. Pingback: Clúster de ESXi 5.5 no publica Storage Policies a vCloud Director 5.5.1 | Capítulos de una jornada hacia la nube

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>