You can create a tag category, tag, and tag association by using the vSphere Automation SDK for PowerShell.

Prerequisites

Verify that you are connected to a vSphere Automation API server.

Procedure

  1. Create a tag category.
    $categoryCreateSpec = Initialize-TaggingCategoryCreateSpec -Cardinality 'MULTIPLE' -AssociableTypes 'VirtualMachine' -Name 'TestCategory' -Description 'TestDescription'
    $categoryId = Invoke-CreateCategory -TaggingCategoryCreateSpec $categoryCreateSpec
    
  2. Create a tag.
    $tagCreateSpec = Initialize-TaggingTagCreateSpec -Name 'TestVMTag' -Description 'This is a test tag for vms' -CategoryId $categoryId
    $tag = Invoke-CreateTag -TaggingTagCreateSpec $tagCreateSpec
    
  3. Create a tag association.
    $vmId = Invoke-ListVM -Names 'MyVM'
    $vmDynamicId = Initialize-StdDynamicID -Type 'VirtualMachine' -Id $vmId
    $tagAssociationBody = Initialize-TaggingTagAssociationAttachRequestBody -ObjectId $vmDynamicId Invoke-AttachTagIdTagAssociation -TagId $tag -TaggingTagAssociationAttachRequestBody $tagAssociationBody