VMware PowerCLI quickly add multiple VM vmnics
PowerCLI is a powerful tool that can be utilized to quickly perform tasks in a VMware environment. ย One of the overly painful tasks that you encounter when configuring or reconfiguring a VM is adding vmnics. ย Especially is this painful if you need to add multiple nics perhaps to many different VMs or specific ones. ย With VMware PowerCLI quickly add multiple VM vmnics is easily performed.
VMware PowerCLI quickly add multiple VM vmnics
The problem with the GUI interfaces is that you can’t really add multiple nics without a lot of clicking and tedious configuration. ย Multiple nic addition is only available in the fat vSphere client and not the Webย client. ย This is limited to (4) vmnics on a new VM.
Additionally, when you bring up a new VM in the Webย client, you are taken to a screen where you can add multiple vmnics, however, it can only be done one at a time (painful).
PowerCLI
In steps PowerCLI. With PowerCLI we can automate the addition of vmnics to any of our virtual machines. The commandlet we use is the new-networkadapter commandlet. We can specify several parameters with this commandlet including the VM we want to add to, the networkname or port group to attach the network adapter to, the type (vmxnet3, etc) andย we can specify we want the network adapter toย startconnected.
new-networkadapter -vm <vmname> -NetworkName "<Port group name>" -Type "VMXNET3" -startconnected
With the above one liner, we can add vmnics to the VM that is specified by theย -vm parameter with the specific parameters we described above.
Multiple vmnics
If we want to addย aย specific number of network adapters to a certain VM, we can do that with aย foreachย loopย with a count statement. ย The below will add (4) new network adapters to a specific VM identified with the -vm parameter.
#Multiple vmnics on a specified VM foreach ($i in 1..4){ new-networkadapter -vm <vmname> -NetworkName "<Port group name>" -Type "VMXNET3" -startconnected } }
Multiple vmnics, Multiple VMs
What though if we want to add (4) new vmnics to a list of VMs? ย We can nest ourย foreach loop inside anotherย foreachย loop and use a variable to read in the contents of a VM list.
#Multiple vmnics on multiple VMs $vms=get-content c:vms.txt foreach ($vm in $vms){ foreach ($i in 1..4){ new-networkadapter -vm $vm -NetworkName "Port group name" -Type "VMXNET3" -startconnected } }
Thoughts
Withย VMware PowerCLI quickly add multiple VM vmnics is easily accomplished either on one specific VM or reading in a list of VMs we want to modify. ย It is a great tool for automation as well as quickly provisioning and making changes that otherwise would be very tedious in the vSphere Web client. ย Long live PowerCLI!