Recently i had to go through tagging some VMs belonging to the same service group. Initially this is to speed up troubleshooting of performance issues within that service group but ultimately they will be used to assist with charge back to service groups/departments. This can be a very time consuming process when completed manually so i decided to script it with a little help from the VMware Technology Network and came up with the following:
#Connect to VI Server
Connect-viserver vcenter1 -user vcenteruser -pass vcenterpassword
New-TagCategory -Name "TEST (Clu1)" -Cardinality "Multiple" -EntityType "DistributedPortGroup, Folder, VirtualMachine"
$Category = Get-TagCategory -Name "TEST (Clu1)"
#Define CSV location
$taginfo = Import-CSV C:\csv\TestClu1.csv
#Create & Apply Tags
ForEach ($item in $taginfo)
$Container = $item.Container
$Name = $item.VMName
Write-Host ".... Assigning Tag to Category $category"
Get-TagCategory -Name $Category | New-Tag -Name $Container | Out-Null
Write-Host ".... Assigning Tag in Category $category to $Name "
Get-Folder $Container | Get-VM | New-TagAssignment -Tag $Container | Out-Null
Sections in red you will need to customize according to where you are saving your CSV. If you have multiple vCenters you will need to run this on each instance with a new CSV for each instance. When creating your CSV the format is as follows:
1,924 total views, 1 views today