There was a requirement in one of the projects to assign Custom Attributes to multiple Virtual Machines hosted in a vSphere Environment. We wrote a PowerCLI script to Assign the Custom Attributes to Virtual Machines using a CSV file which had all the details of the Custom Attributes.
Requirements:
- PowerCLI
- PowerShell 5.1 or later
Script:
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
$cred = Get-Credential
Connect-VIServer vCenter_Server_IP_Address/FQDN -Credential $cred
$files = Import-CSV "C:\Users\testuser\Desktop\CustomAttributes.csv"
foreach ($file in $files)
{
$vm = Get-VM -Name $file.VMName
$vm | Set-Annotation -CustomAttribute "AppName" -Value $file.AppName
$vm | Set-Annotation -CustomAttribute "Owner" -Value $file.Owner
$vm | Set-Annotation -CustomAttribute "AppDL" -Value $file.AppDL
$vm | Set-Annotation -CustomAttribute "Env" -Value $file.ENv
}
Disconnect-VIServer -Confirm:$false
Input CSV File:

Just replace the vCenter_Server_IP_Address/FQDN and Path of CSV File and run the script to assign Custom Attributes to Virtual Machines in vSphere environment. BOOM!!
Happy Scripting!!