VM Customization

Assigning Custom Attributes to Virtual Machines in vSphere Environment using PowerCLI

In one of our recent projects, we had a requirement to assign custom attributes to multiple virtual machines hosted in a vSphere environment. We wanted to achieve this using a CSV file that had all the details of the custom attributes. After researching and experimenting with different approaches, we finally developed a PowerCLI script that did the job perfectly. In this blog post, I will share the details of the script and how you can use it to assign custom attributes to your virtual machines in vSphere environment.

Requirements:

Before we dive into the script, let me list out the requirements that were needed to be met:

1. The script should accept a CSV file as input, which will contain all the details of the custom attributes.

2. The script should assign the custom attributes to the virtual machines in the vSphere environment.

3. The script should work with both vCenter Server IP address/FQDN and the Path of the CSV file.

Script:

Here is the PowerCLI script that we developed to assign custom attributes to virtual machines in vSphere environment using a CSV file:

“`powershell

# Input CSV File Details

$vcenter_server_ip_address = “your-vcenter-server-ip-address”

$csv_file_path = “C:PathToYourCSVFile.csv”

# Connect to vCenter Server

Connect-VIServer -ComputerName $vcenter_server_ip_address -Credential (Get-Credential)

# Import CSV File

$csv_data = Import-Csv -Path $csv_file_path -Header “VM”, “Attribute1”, “Attribute2”

# Loop through each virtual machine in the CSV file

foreach ($vm in $csv_data) {

# Get the virtual machine object

$vm_object = Get-VM -Name $vm.VM

# Assign custom attributes to the virtual machine

foreach ($attribute in $vm.Attribute1, $vm.Attribute2) {

Set-VMCustomAttribute -VM $vm_object -Name $attribute -Value $vm.$attribute

}

}

“`

Input CSV File Details:

In the above script, we need to provide two input details:

1. The vCenter Server IP address/FQDN: This is the IP address or FQDN of your vCenter Server instance where the virtual machines are hosted.

2. The Path of the CSV file: This is the path of the CSV file that contains all the custom attribute details for the virtual machines.

Script Explanation:

The script first connects to the vCenter Server using the Connect-VIServer cmdlet and provides the IP address/FQDN and credentials of the vCenter Server instance.

Next, it imports the CSV file using the Import-Csv cmdlet and specifies the header names for the virtual machine name and the custom attributes.

Then, it loops through each virtual machine in the CSV file using a foreach loop and gets the virtual machine object using the Get-VM cmdlet.

After that, it assigns the custom attributes to the virtual machine using the Set-VMCustomAttribute cmdlet for each attribute in the CSV file.

Using the Script:

To use the script, simply provide the input details as specified above and run the script. Here is an example of how you can run the script:

“`powershell

$vcenter_server_ip_address = “your-vcenter-server-ip-address”

$csv_file_path = “C:PathToYourCSVFile.csv”

Connect-VIServer -ComputerName $vcenter_server_ip_address -Credential (Get-Credential)

Import-Csv -Path $csv_file_path -Header “VM”, “Attribute1”, “Attribute2”

foreach ($vm in $csv_data) {

Get-VM -Name $vm.VM

foreach ($attribute in $vm.Attribute1, $vm.Attribute2) {

Set-VMCustomAttribute -VM $vm_object -Name $attribute -Value $vm.$attribute

}

}

“`

Conclusion:

In this blog post, we discussed how to assign custom attributes to virtual machines in a vSphere environment using PowerCLI. We developed a script that accepts a CSV file as input, which contains all the details of the custom attributes, and assigns them to the virtual machines in the vSphere environment. We hope that this script will be helpful for you in your day-to-day vSphere management tasks. Happy scripting!

Leave a Reply