Thursday, November 12, 2009

Setting Custom Attributes in VMware programtically

If you have more than a few VMs in your ESX environment, you have already found a need to properly organize the VMs with folders and hierarchies. However, this structure goes away when you view all the VMs in your datacenter, making your carefully created tree structure useless.

The good news is that VMware has a "Custom Attribute" option for each VM. Displayed next to the Notes field, this allows you to define attributes like Customer, Department, Owner, Production Status or anything else you can imagine to tag every system in your environment. The question is - how to do this without manually typing in the attribute for each VM: and in comes powershell.

Assuming you want to create an attribute to match your folder structure, you can use the Get-Vm -Location powershell command to retrieve a list of all VMs in a folder (and sub folders). Pipe this output into the Set-CustomField command and let the computer do the work for you. An example of this is below

Get-Vm -Location 'App X' | Set-CustomField -Name 'System Function' -Value 'App X'

You can use other switches with Get-Vm to filter on name, datastores, host servers, and other options.

No comments: