VDM30in30 – Day 4 – How to get some easy wins with Powershell when provisioning – part 2


Ok, need to catch up on some blog posts here, been busy.

I told you we would dive into Powershell / PowerCLI / Powertool, so here is a starting point for reference. I managed to get one of these posters when it was being provided to vExperts, and I’m quite happy with it as an always there reference guide. Below is the PDF of this poster if you want to keep it on-hand or print it out in large format.



Understanding the basic syntax is important before working with advanced scripts and automation. So the first thing is to connect to the host or vCenter server.

PowerCLI can be used to connect to both vCenter and also the vSphere host independently.

How to connect

If you are connecting to either a vCenter server or a vSphere Host the cmdlet is the same, you can use the Connect-VIServer cmdlet to connect to both of these (even at the same time), lets take a look at an example:

Connect-VIServer -Server vchost01 -User admin -Password pass

In the above example you can see we are connecting to our vCenter server called “vchost01” with a username and password to gain access to the vCenter server, we did not specify a protocol or port, by default HTTPS and port 443 is assumed which is the same as the vSphere Client or Web Client, unless you specify a –port or –protocol parameter for the cmdlet.

Now that your connected, lets work with some fun stuff.

Using vSphere Tags with PowerCLI

  • One for exporting/importing of tags
  • One for converting the legacy custom attributes to tags

Managing Tag Categories

Tag Categories allow you to group related tags together. When you define a category, you can also specify which object types its tags can be applied to and whether more than one tag in the category can be applied to an object. Creating a category with PowerCLI is easy:

New-TagCategory –Name [name] -Description [description] -Cardinality [single/multiple] –EntityType [list of types]

For example let’s create a category that will contain a tag for each vSphere user. We’ll name this category “Owner” and use it to tag the owners of Virtual Machines. The tags will only be applicable to Virtual Machines and a Virtual Machine can have only one owner. The following script creates such a category:

New-TagCategory –Name “Owner” –Cardinality single –EntityType VirtualMachine

The “-Cardinality” parameter specifies whether a single or multiple tags from this category can be applied to the same object at the same time. If you don’t specify it the default is “single”. The “-EntityType” parameter allows you to specify the object types to which you can attach tags from this category. If you omit the parameter by default the category will be applicable to all supported entity types.

The full list of supported entity types is: VirtualMachine, VM, VMHost, Folder, Datastore, DatastoreCluster, Cluster, ResourcePool, DistributedSwitch, DistributedPortGroup, VirtualPortGroup, VApp, Datacenter, All.

Modifying an existing TagCategory is done through Set-TagCategory cmdlet. You can change its name, description, cardinality (you can only extend it to “multiple”, restricting it to “single” is not possible) and add more entity types (again you can only extend the applicable entity types).

In our previous example the “Owner” category is only applicable to Virtual Machines, however VApps can also have an owner. Let’s modify the category to allow tagging VApps as well:

Get-TagCategory “Owner” | Set-TagCategory –AddEntityType VApp

Removing an existing category is just as easy by using the Remove-TagCategory cmdlet. Note that by removing a category you are also removing all tags in it and any assignments of these tags! Here is an example:

Remove-TagCategory “Owner”

Managing Tags

Once you have a tag category you are able to create new tags in it. This is done through the New-Tag cmdlet. For example in our “Owner” category let’s create a tag for John Smith:

New-Tag –Name “jsmith” –Category “Owner”

You can also create multiple tags – by reading the input values from CSV file or from other cmdlets. Here is how to create tags for each user in the “Example.org” domain:

# Retrieve all user accounts in the “Example.org” domain

$userList = Get-VIAccount –User –Domain “Example.org”

# For each user account create a new tag based on the user’s Id

foreach ($user in $userList) { New-Tag –Category “Owner” –Name $user.Id –Description $user.Description }

Modifying an existing tag is done through the Set-Tag cmdlet. It allows you to change the tag’s name and description:

Get-Tag “jsmith” –Category “Owner” | Set-Tag –Description “John Smith”

Removing a tag is done through Remove-Tag cmdlet, similar to removing a category. When removing a tag you will automatically remove any assignments of this tag.

Now that you have created some tags – it’s time to put them to use. You can assign tag to an entity using the New-TagAssignment cmdlet. Here is how to assign the “jsmith” tag to the VMs that belong to John (we assume they have “jsmith” in their name):

Get-VM –Name *jsmith* | New-TagAssignment –Tag “jsmith”

You can easily retrieve all tag assignments on a given entity:

Get-VM jsmith_vm1 | Get-TagAssignment

Or you can retrieve all VMs that have a given tag associated with them:

Get-VM –Tag “jsmith”

If you want to build a report that displays information about your inventory objects and all tags associated with them – you can do that by using the New-VIProperty cmdlet to add a “Tag” property to your objects. Here is an example of how to do that for the VirtualMachines, but it is easily applicable to other types as well:

# First we want to define the new “Tag” property of the VirtualMachine object

New-VIProperty -Name Tag -ObjectType VirtualMachine -Value `

{ Get-TagAssignment -Entity $args[0] | select -ExpandProperty Tag }

# Now retrieve all VMs and their tags:

Get-VM | select Name, Tag

So why would you want to use tags?

Tags are meta-data for objects in vSphere. You can associate them to cost centers, departments, application groups, datacenters, etc. Then you can select the tags as a source to apply an action to. You can use this for automation, making your life easier with day-day activities, or inventory and policy based activities.

Here is a good video on why you would want to use tags and what you can do with them.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s