Reporting Tags

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.