For vSphere tagging, most PowerCLI users go for the “Get-Tag”,”Get-TagCategory”, or “Get-TagAssignment” methods.
Depending on the size of your vCenter, inventory count, but more importantly, Tag count, the CisService methods may provide better performance for vSphere Tagging.
To access these APIs, you will need to log in to vcenter with the following string. (Adjust vcenter/user/pass as needed)
connect-cisserver “vcenter.pcli.me” -username “administrator@vsphere.local” -pass “mypassword”
To start, each method has documentation. I will say, it’s not extremely easy to read, but its better than leaving us stranded.
I will show you how to pull the command documentation for Tag Categories and you should be able to translate this for other methods.
Pasting the following two lines will show you the “Operations” you can complete within the “tagging.category.”
$allCateMethod = Get-CisService com.vmware.cis.tagging.category
$allCateMethod.Help
From here, you can expand the help. Lets say you want to create a new Tag Category. You add on “create” to the help command and it will show you what you need to complete the command.
$allCateMethod.Help.create
$allCateMethod.Help.create.create_spec
Below, I will post the most common methods I use.
# Create a new Tagging Category: (I included all possible options for the specs after the comment hash)
$allCateMethod = Get-CisService com.vmware.cis.tagging.category
$spec = $allCateMethod.Help.create.create_spec
$spec.name = “myFirstCategory”
$spec.description = “”
$spec.cardinality = “MULTIPLE” # “SINGLE” # yes it needs to be all caps….
$spec.associable_types = “virtualmachine”,”vmhost” # Cluster, Datacenter, Datastore, DatastoreCluster, DistributedPortGroup, DistributedSwitch, Folder, ResourcePool, VApp, VirtualPortGroup
$allCateMethod.Create($spec)
# Get all Tag Categories:
$allCateMethod = Get-CisService com.vmware.cis.tagging.category
$allcate = $allcateMethod.list()
$cates = @()
foreach ($cate in $allcate) {
$cates += $allCateMethod.Get($cate)
}
$cates
# Create a new Tag: (a Tag needs a category so be sure to create one first)
$alltagMethod = Get-CisService com.vmware.cis.tagging.tag
$spec = $alltagMethod.Help.create.create_spec
$spec.name = “myFirstTag”
$spec.description = “”
$spec.category_id = $cates | where {$_.name -eq “myFirstCategory”} | %{$_.id.value}
$alltagMethod.Create($spec)
# Get all Tags:
$alltagMethod = Get-CisService com.vmware.cis.tagging.tag
$alltag = $alltagMethod.list()
$tags = @()
foreach ($tag in $alltag) {
$tags += $alltagMethod.Get($tag)
}
$tags
# From here, you can now Attach, Detach, or Query tags to objects. To access those objects (like a Virtual Machine) you will need to connect to vcenter with our VIServer connection.
connect-viserver “vcenter.pcli.me” -username “administrator@vsphere.local” -pass “mypassword”
Now we can pull in object IDs and match them with Tag IDs.
# Attach a Tag to a VirtualMachine:
$mytag = $tags | where {$_.name -eq “myFirstTag”}
$vm = Get-VM -Name “myFirstVM”
$tagAssign = Get-CisService -Name com.vmware.cis.tagging.tag_association
$spec = $tagAssign.Help.attach.object_id.Create()
$spec.type = $vm.ExtensionData.MoRef.Type
$spec.id = $vm.ExtensionData.MoRef.Value
$tagAssign.attach($mytag.id.Value, $spec)
# Then to check the Association:
$tagAsso = Get-CisService -Name com.vmware.cis.tagging.tag_association
$tagAsso.list_attached_objects($mytag.id.value)
If you dig deep into the documentation of these methods, you will find ways to Add, Delete, Create, and even Batch assign tags. I gave you the starting point here.
One extra for the road:
# Quick method to delete a Tag Category: (Quick note…. Deleting a category WILL delete all of the tags under it and the API call will not prompt you about it.)
$mycateID = $cates |where {$_.name -eq “myFirstCategory”} |%{$_.id.value}
$allCateMethod = Get-CisService com.vmware.cis.tagging.category
$allcateMethod.Delete($mycateID)
Let me know if you have any questions @vmnick0 on twitter.