• hi@yahyazahedi.com
  • Germany

Add VMkernel adapter to a number of ESXi hosts

Currently, I’m working with a customer who was wanting to add new VMkernel to their ESXi hosts and the number of ESXi hosts is about 50. Doing this task by vSphere Web Client was a bit time consuming and it is worth to write a script to this task automatically in a few seconds.
While I was working on scripting I faced different problems and decided to share my experience with you!
We are using the PowerCLI command to create the VMkernel Ports. If you have not Installed PowerCLI in PowerShell, See the following link.

In order to add new VMkernel for ESXi host, we utilize New-VMHostNetworkAdapter which is powerful command for creating a new HostVirtualNIC (Service Console or VMKernel) on the specified host.

We defined the input parameters of this command by variables. For example in our scenario, which in my lab, I have 3 ESXi hosts that I want to create a VMKernel for VMotion with the following IP range.

ESXi-A.*****.Localvmk3100.10.10.1255.255.255.0
ESXi-B.*****.Localvmk3100.10.10.2255.255.255.0
ESXi-C.*****.Localvmk3100.10.10.3255.255.2550

I have created a very very simple PowerShell script with only 8 lines which are easy to understand.

Connect-VIServer vcsa.*****.local -User y.zahedifard@*****.local -Password "********"
 $hosts=Get-VMHost
 $i=0
 1,2,3 | foreach { 
     $IP = "100.10.10.$_"
     $host1= $hosts[$i]
     New-VMHostNetworkAdapter -VMHost $host1 -PortGroup "vMotion-B" -virtualSwitch "vDS-01" -IP $IP -SubnetMask 255.255.255.0
     $i=$i+1 }

Line 1: First you need to connect the vCenter server.
Line 2: Get the list of all ESXi hosts which is managed by this vCenter.
Line 3: A pointer for specifying a host in the loop.
Line 4: create a loop for giving different IP addresses for each VMKernel, you can specify the numbers by commas or range like this 1..3.
Line 5: The last octet of the IP address change by numbers we defined at line 4.
Line 6: Get the first ESXi host of the list by calling $hosts[0], in the next loop it will increase by 1 (Line8).
Line 7: Creates a new VMKernel network adapter and connects it to the specified port group on the specified distributed switch with a specific IP address.
Line 8: increase the pointer to point out the next ESXi host.

Share Post on:

Leave a Reply

Your email address will not be published. Required fields are marked *