F
or a couple of releases now PowerCLI has been able to work with vSphere Tags, A tag is a label that you can apply to objects in the vSphere inventory.
After creating a tag, you can assign that tag to a category. Categories allow you to group related tags together. When you define a category, you can also specify the type of objects to which its tags can be applied to and whether more than one tag in the category can be applied to an object.
For example, if you want to tag your virtual machines by the owner, you can create a category called “Owner” and specify that it applies to virtual machines only and that only a single tag can be applied to a virtual machine at any time. The tags in this category could be Alan, John or Fred etc.
I have had a few people ask me how they can use PowerCLI to work with external systems, CMDBs, databases or even just a CSV file.
One example of this is where a company could have various information about hosts or datastores or virtual machines, like the project that purchased these, a cost code or an owner. This data is generally stored somewhere else but it would be great to see this information straight in the vSphere Web Client where you manage the objects so that you can instantly contact the owner or work out which project the object is being used for etc.
The below video shows how we can use PowerCLI and this generic script I created to import the data, create the tags and tag categories and assign them to the machines, it uses a csv as input but this could obviously be changed to anything which can be read in PowerShell, like a API, database, application etc etc.
Automating tags and tag categories video
Example Script
This script is the script I created as an example which relates each of the items in the Name column to an object in the inventory then for each of the other column headers it will create a category and then the tags that are under the categories, once this has been done it will apply the tags to the objects in the Name column.
Connect-viserver myvc.corp.local -user administrator@vsphere.local -pass Pa$$w0rd
$CMDBInfo = Import-CSV C:\Software\cmdbinfo.csv
# Get the header names to use as tag category names
$TagCatNames = $cmdbinfo | Get-Member | Where {$_.MemberType -eq "NoteProperty"} | Select -Expand Name
# Create the Tag Category if it doesnt exist
Foreach ($Name in ($TagCatNames | Where {$_ -ne "Name"})) {
if (-Not (Get-TagCategory $Name -ea SilentlyContinue)) {
Write-Host "Creating Tag Category $Name"
New-TagCategory -Name $Name -Description "$Name from CMDB" | Out-Null
# Create Tags under the Tag Categories
$UniqueTags = $cmdbinfo | Select -expand $name | Get-Unique
Foreach ($Tag in $UniqueTags) {
if (-Not (Get-Tag $Tag -ea SilentlyContinue)) {
Write-Host "..Creating Tag under $Name of $Tag"
New-Tag -Name $Tag -Category $name -Description "$Tag from CMDB" | Out-Null
}
# Assign the Tags to the VMs/Hosts
$cmdbinfo | Where {$_.($Name) -eq $Tag} | Foreach {
Write-Host ".... Assigning $Tag in Category of $Name to $($_.Name)"
$TagAssignment = Get-Tag -Category $Name -name $Tag
New-TagAssignment -entity $($_.Name) -Tag $Tagassignment | Out-Null
}
}
}
}

Are you interested in PowerShell? Do you want free Pizza? If the answer to either of these questions is YES then join us!