Getting a vSphere VM’s Network Adapters

By | March 26, 2013

Getting all the network adapters for a VM is an easy task:

When you remove a network adapter, vSphere will remove it’s entry in the .vmx file. In the .vmx file, the adapter configuration options are prepended by an “ethernetX” string, where X is a number between 0 and 9. Removing the adapter, removes all the appropriate “ethernetX” entries from the file. Adding an adapter to a VM will use the first available number, so if in a 5 NIC VM “ethernet2” is not present, this is where the adapter will be placed. This has the effect of calling the NIC “Network Adapter 3”.

Why does this matter ? Because we want to get our adapters in a reliable order, regardless of the number of NICs present.

The above line will work for all VM’s with less than 10 NICs. When we have 10 NICs we need to do a little bit of regex on the sort code:

This strips out the number from the name of the adapter, converts it to an int and sorts based upon that.

This was all done with PowerCLI

Sidenote: I really need to find a decent plugin for code highlighting :-/