Generate Tags Automatically by Using a Script

You can use a script to generate tags automatically. For example, you can create a virtual machine owner tag for each user account in a domain.

You must use the Get-VIAccount cmdlet to retrieve user accounts. For more information, see the documentation of the cmdlet.

Prerequisites

  • Verify that you are connected to a vCenter Server system.
  • Verify that the user accounts and the vCenter Server system are in the same domain.

Procedure

  1. Create a new tag category and specify that tags in this category can only be assigned to entities of type VirtualMachine.
    $ownerTagCategory = New-TagCategory -Name Owner -EntityType VirtualMachine
    Note: If you do not specify an entity type, tags from this category can be assigned to all entity types.
  2. Retrieve all domain user accounts and save them in a variable.
    $accounts = Get-VIAccount -User -Domain 'DomainName' -Category | select -ExpandProperty Id
  3. Create a tag for each user account.
    $accounts | foreach { New-Tag -Category $ownerTagCategory -Name $_ }
  4. Retrieve a specific tag from the Owner category, so that you can later assign it to a specific virtual machine.
    $ownerTag = Get-Tag -Category $ownerTagCategory -Name 'John_Smith'