Configuring the port group of each VM in a specific vCenter folder
Recently, I created a simple PowerCLI script to configure the port group, or “network label”, for each VM within a specific vCenter folder. Having a method to quickly perform this task is valuable in preparing to transition from a pilot to production implementation of VMware View. In planning for the production deployment of a virtual desktop infrastructure (VDI), our network engineer has carved out an organized IP address space within the corporate LAN. The logical result is that each department in the company will have their own assigned VLAN/Subnet. This model lends itself nicely to the approach taken in the organized structure of desktop pools implemented in vCenter. The folder hierarchy we’ve created in vCenter mimics the corporate structure, including each department and group within. The related desktop pools are then placed in the appropriate folder upon creation.
Throughout the duration of the pilot we had just left the network label per VM on one common port group for simplicity. As a result, the 45 VMs we have created since the project began need to have their port group configured appropriately. So, I had to figure out how to easily link the networking and organizational attributes of our VDI environment. Luckily, we have the folder structure in place to make the scripting possible, and easy.
The major motivation for creating the script was to save a vast amount of the administrator’s time (my time). This script would be helpful in both retroactively configuring the existing VMs as well as future ones.
This is the script I came up with, it’s about 5 lines of code and works quite nicely by reading the required data from a provided CSV file. The structure of the CSV file includes the folder name and desired port group to assign to VMs within that folder.
$folders = Import-CSV 'path_to_csv';
foreach($item in $folders){
$folder = $item.folder;
$portgroup = $item.portgroup;
get-vm -location $folder|Get-NetworkAdapter|Set-NetworkAdapter -NetworkName $portgroup -Confirm:$false;
}
I’m looking forward to using this folder-level configuration script for a variety of other tasks that will be helpful within a VDI environment.
Thanks for reading,
- Arin
10 months ago • 4 notes