Add VMkernel adapters in a specific netstack via PowerCLI (vSphere 6.5)

Currently i’m working at a customer on a ‘Host Provisioning’ automation script. The goal is to automate all the necessary steps to take the host in production. While i was working on scripting things i ran into a problem which cost me quite some time. But i figured it out!Since this customer is using there vMotion and Provisioning VMkernel adapter in the ‘vMotion’ and ‘Provisioning’  TCP/IP stacks i could not use the default PowerCLI command to create the VMkernel ports. If you use the default, the VMkernel adapter is created in the default TCP/IP Stack.

The first thing you need to do in PowerCLI is use the ‘esxcli’ commands (version 2) in order to create the the VMkernel adapters in the appropriate netstack. Explained in detail on Virten.net

$esxcli = Get-EsxCli -VMhost (Get-VMHost $esxiserver) -V2

Connect to vCenter

Connect-VIServer $vcenterserver -User $vcenteradmin -Password $vcenteradminpw -Force

Use esxcli to add the vMotion VMkernel adapter in a specific netstack on the ESXi host.

$esxcli.network.ip.interface.add.Invoke(@{interfacename = ‘vmk6’; portgroupname = ‘VM Network’; netstack = ‘vmotion’})

I also needed to add the Provisioning VMkernel adapter on the ESXi host. Again, in the specific ”Provisioning” netstack.

$esxcli.network.ip.interface.add.Invoke(@{interfacename = ‘vmk9’; portgroupname = ‘VM Network’; netstack = ‘vSphereProvisioning’})

This is where i ran into an issue. By running this command the Powershell prompt spitted out the following error message:
vmkernel_creation_error

It cost me some time to figure out why this was happening. But apparently the Provisioning and vMotion stacks are not by default existing on an ESXi host.  Only the ‘default’ is existing. When  you create a new VMkernel adapter via the vSphere Web Client the ‘Provisioning’ stack is being created at the same time.

default_tcpstack

The reason that i failed to add the VMkernel adapter is now obvious. And the solution has been suddenly become very simple. Just create the ‘Provisioning’ and ‘vMotion’ stack myself prior to adding the VMkernel adapters.

I did this also with the esxcli command from Powershell.

$esxcli.network.ip.netstack.add.Invoke(@{netstack = ‘vmotion’})

$esxcli.network.ip.netstack.add.Invoke(@{netstack = ‘vSphereProvisioning’})

Since the ‘Provisioning’  and vMotion TCP/IP stacks are now existing on ESXi even when there is no actual VMkernel adapter configured on the host, I am able to create the VMkernel adapters for both the vMotion and Provisioning stack via PowerCLI.

Here is the complete script I used to create the netstacks, VMkernel adapters and migration of the adapters to a vSphere Distributed Switch.

Get-Module -ListAvailable VMware* | Import-Module

##Infra-Info##

$vcenterserver = ‘vcsa02.wesleygeelhoed.local’
$vcenteradmin = ‘administrator@vwees.local’
$vcenteradminpw = ‘*****’
$esxiserver = Read-Host -Prompt ‘Provide the FQDN of the ESXi host you want to add’
$esxiuser = ‘root’
$esxiuserpw = ‘******’
$vmwaredatacenter = ‘DR’
$cluster = ‘SOYOUSTART’
$dvSwitch = “dvSwitch”
$vmotionpg = “VLAN 88”
$vmotionmask = “255.255.255.0”
$vsanmask = “255.255.255.0”
$provisioningmask = “255.255.255.0”
$vsanpg = “VLAN 88”
$managementpg = “VLAN 88”
$DNS = “192.168.77.201”, “192.168.77.180”
$NTP = “192.168.77.201”
$vsanip = “”
$cachingssd = “”
$capacitydisk = “”
$domain = ‘wesleygeelhoed.local’

##Connect to vCenter##
write-host Connecting to vCenter Server instance $vcenterserver -ForegroundColor Yellow
Connect-VIServer $vcenterserver -User $vcenteradmin -Password $vcenteradminpw -Force

##Add Hosts to vCenter##
write-host Start adding ESXi hosts to the vCenter Server instance $vcenterserver -ForegroundColor Yellow
Add-VMHost $esxiserver -Location $vmwaredatacenter -User $esxiuser -Password $esxiuserpw -Force

##Put Host in Maintenance Mode##
write-host Put $esxiserver in Maintenance mode
Set-VMhost $esxiserver -State Maintenance

##ESXCLI##
$esxcli = Get-EsxCli -VMhost (Get-VMHost $esxiserver) -V2

##Create netstack and add VMkernel adapters##
write-host Create Provisioning and vMotion TCP/IP stack and add vMotion and Provisioning VMkernel ports -ForegroundColor Yellow
$esxcli.network.ip.netstack.add.Invoke(@{netstack = ‘vmotion’})
$esxcli.network.ip.netstack.add.Invoke(@{netstack = ‘vSphereProvisioning’})
$esxcli.network.ip.interface.add.Invoke(@{interfacename = ‘vmk1’; portgroupname = ‘VM Network’; netstack = ‘vmotion’})
$esxcli.network.ip.interface.add.Invoke(@{interfacename = ‘vmk2’; portgroupname = ‘VM Network’; netstack = ‘vSphereProvisioning’})

##Add Host to Distributed Switch##
Write-host adding $esxiserver to the $dvSwitch -ForegroundColor Yellow
Get-VDSwitch -Name $dvSwitch | Add-VDSwitchVMHost -VMHost $esxiserver

##Add 2nd NIC to Distributed Switch##
$vmhostnetworkadapter2 = Get-VMhost $esxiserver | Get-VMHostNetworkAdapter -Physical -Name vmnic1
Write-host adding $vmhostnetworkadapter2 to $dvSwitch -ForegroundColor Yellow
Get-VDSwitch -Name $dvSwitch | Add-VDSwitchPhysicalNetworkAdapter -VMHostPhysicalNic $vmhostnetworkadapter2 -Confirm:$false | Out-Null

##Migrate MGMT vmk to Distibuted Portgroup##
$destinationmgmtpg = Get-VDPortgroup -Name “VLAN 88”
write-host Migrate Management Network to $dvSwitch -ForegroundColor Yellow
$dvportgroup = Get-VDPortgroup -Name $destinationmgmtpg -VDSwitch $dvSwitch
$vmk = Get-VMHostNetworkAdapter -Name vmk0 -VMHost $esxiserver
Set-VMHostNetworkAdapter -PortGroup $dvportgroup -VirtualNic $vmk -Confirm:$false | Out-Null

##Migrate vMotion vmk to Distibuted Portgroup##
$destinationvmotionpg = Get-VDPortgroup -Name “VLAN 88”
write-host Migrate vMotion VMkernel to $dvSwitch -ForegroundColor Yellow
$dvportgroup = Get-VDPortgroup -Name $destinationvmotionpg -VDSwitch $dvSwitch
$vmk = Get-VMHostNetworkAdapter -Name vmk1 -VMHost $esxiserver
Set-VMHostNetworkAdapter -PortGroup $dvportgroup -VirtualNic $vmk -Confirm:$false | Out-Null

##Migrate Provisioning vmk to Distibuted Portgroup##
$destinationprovisioningpg = Get-VDPortgroup -Name “VLAN 88”
write-host Migrate Provisioning VMkernel to $dvSwitch -ForegroundColor Yellow
$dvportgroup = Get-VDPortgroup -Name $destinationprovisioningpg -VDSwitch $dvSwitch
$vmk = Get-VMHostNetworkAdapter -Name vmk2 -VMHost $esxiserver
Set-VMHostNetworkAdapter -PortGroup $dvportgroup -VirtualNic $vmk -Confirm:$false | Out-Null

##Add first NIC to Distibuted Switch##
$vmhostnetworkadapter1 = Get-VMhost $esxiserver | Get-VMHostNetworkAdapter -Physical -Name vmnic0
Write-host adding $vmhostnetworkadapter1 to $dvSwitch -ForegroundColor Yellow
Get-VDSwitch -Name $dvSwitch | Add-VDSwitchPhysicalNetworkAdapter -VMHostPhysicalNic $vmhostnetworkadapter1 -Confirm:$false | Out-Null

##Remove Virtual Standard Switch##
Write-Host Removing the Virtual Standard Switch -ForegroundColor Yellow
Get-VirtualSwitch -VMHost $esxiserver -Name vSwitch0 | Remove-VirtualSwitch -Confirm:$false

Sources: vmware.com, virten.net

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s