VMware Cloud Community
Warren89
Contributor
Contributor
Jump to solution

Setting vMotion TCP/IP stack when making vMotion VMKernel interface with Powershell

Hello,

I am working on scripting the creation of vMotion VMkernel interfaces and need to set the TCP/IP stack to vMotion during the process. I am able to successfully make the VMKernel interfaces but have not had much luck with changing the TCP/IP stack using my script. Below is the code I am using so far and I am open to any suggestions. 

#vMotion VMKernel Interface Data
$vMotion1IP = read-host "Enter IP of 1st vMotion VMKernel Interface"
$vMotion1Subnet = read-host "Enter SubnetMask of 1st vMotion VMKernel Interface"

$vMotion2IP = read-host "Enter IP of 2nd vMotion VMKernel Interface"
$vMotion2Subnet = read-host "Enter SubnetMask of 2nd vMotion VMKernel Interface"

$VirtualSwitch = Get-VirtualSwitch -VMHost $VMHost -name vswitch0


New-VMHostNetworkAdapter -VMHost $VMHost -PortGroup vMotion1 -VirtualSwitch $VirtualSwitch -IP $vMotion1IP -SubnetMask $vMotion1Subnet

 

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

That means that the NetworkStack parameter doesn't support OBN (Object By Name).
Instead, try using the object returned by Get-VMHostNetworkStack, i.e.

 

 

$stack = Get-VMHostNetworkStack -VMHost $VMHost -Name 'vmotion'
New-VMHostNetworkAdapter -VMHost $VMHost -VirtualSwitch $VirtualSwitch -PortGroup 'S-vMotion1' -IP $S_vMotion1IP -SubnetMask $S-vMotion1Subnet -NetworkStack $stack

 

Updated: fixed the typo

 

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
18 Replies
LucD
Leadership
Leadership
Jump to solution

Not really sure what your question actually is, but there is a NetworkStack parameter on the New-VMHostNetworkAdapter cmdlet.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Warren89
Contributor
Contributor
Jump to solution

Sorry if my original post was not clear.

When my script makes the VMKernel interface I would like to set its TCP/IP stack to vMotion instead of Default if that makes more sense. 

Tags (1)
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Then you should use the NetworkStack parameter


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
kwhornlcs
Enthusiast
Enthusiast
Jump to solution

My code on this is probably a little dated based on LucD's reply but I've used it for a long time as part of automating vmhost intake and it's never failed me. The data for the adapter portgroups and gw's come from a hash table presumed in the larger overall script but could come from other means. There was a reason for why I had the intermediate step of creating a local temporary vSwitch but have long forgotten why, and that may no longer be needed.

For context it sets up two vmk adapters for layer 3 (routed) vmotion across two different port groups - in this case separate channels on a UCS environment.

$esxcli = Get-EsxCli -VMhost $virtualHost -V2

##create temporary switch
$tempvswitch = $virtualHost|New-VirtualSwitch -Name “temp”
Start-Sleep -Seconds 2
New-VirtualPortGroup -VirtualSwitch $tempvswitch -Name “temp” -VLanId “4000” | Out-Null
Start-Sleep -Seconds 2

##enable vmotion stack
$esxcli.network.ip.netstack.add.Invoke(@{netstack = 'vmotion'; disabled = $false})| Out-Null

##Create first vmotion adapter
$esxcli.network.ip.interface.add.Invoke(@{interfacename = 'vmk1'; portgroupname = 'temp'; netstack = 'vmotion'})| Out-Null
$esxcli.network.ip.interface.ipv4.set.Invoke(@{interfacename = 'vmk1'; ipv4 = $HostRecord.vmip1; netmask = $vcsettings.vmtnMask; type = 'static'})| Out-Null
Start-Sleep -Seconds 10
$vmhost | Get-VMHostNetworkAdapter -Name 'vmk1' | Set-VMHostNetworkadapter -PortGroup $vmtn1PG -Confirm:$false | Out-Null
Write-Host "`t...vmk1 added to host" @Warn
Start-Sleep -Seconds 5

##Create Second vmotion adapter
$esxcli.network.ip.interface.add.Invoke(@{interfacename = 'vmk2'; portgroupname = 'temp'; netstack = 'vmotion'})| Out-Null
$esxcli.network.ip.interface.ipv4.set.Invoke(@{interfacename = 'vmk2'; ipv4 = $HostRecord.vmip2; netmask = $vcsettings.vmtnMask; type = 'static'})| Out-Null
Start-Sleep -Seconds 10
$vmhost | Get-VMHostNetworkAdapter -Name 'vmk2' | Set-VMHostNetworkadapter -PortGroup $vmtn2PG -Confirm:$false | Out-Null
Write-Host "`t...vmk2 added to host" @Warn

##delete temporary switch
$tempvswitch | Remove-VirtualSwitch -Confirm:$false
Start-Sleep -Seconds 2
$esxcli.network.ip.route.ipv4.add.Invoke(@{netstack = 'vmotion'; gateway = $vcsettings.vmtnGW; network = 'default'})| Out-Null

 

Reply
0 Kudos
mrtgokul
Contributor
Contributor
Jump to solution

Heloo Warren,

I found this Esxcli method in one of the LucD's reply : https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/TCP-IP-Stack/m-p/1785671

You can check the below code which might help you

#vMotion VMKernel Interface Data
$vMotion1IP = read-host "Enter IP of 1st vMotion VMKernel Interface"
$vMotion1Subnet = read-host "Enter SubnetMask of 1st vMotion VMKernel Interface"

# This Creates a vMotion Kernal adapter with vMotion TCP/IP Stack using Esxcli

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

$sVmk = @{

interfacename = 'vmk2'

portgroupname = 'VMPortGroup'

netstack = 'vmotion'

}

$esxcli.network.ip.interface.add.Invoke($sVmk)


$VirtualSwitch = Get-VirtualSwitch -VMHost $VMHost -name vswitch0

# Sets the IP and Subnet mask for the above vMotion Kernel adapter

Get-VMHostNetworkAdapter -VMHost $vmhost -VirtualSwitch $VirtualSwitch -VMKernel | Where-Object {$_.VMotionEnabled -eq $true} | Set-VMHostNetworkAdapter -IP $vMotion1IP -SubnetMask $vMotion1Subnet

 

Reply
0 Kudos
Warren89
Contributor
Contributor
Jump to solution

Update:

I appreciate all the responses but I am still not having much luck. Below is what I have been able to put together along with the error I am getting. 

###################################################################################

#vMotion VMKernel Interfaces data
$S_vMotion1IP = read-host "Enter IP of 1st vMotion VMKernel Interface"
$S_vMotion1Subnet = read-host "Enter SubnetMask of 1st vMotion VMKernel Interface"

###################################################################################

#ESXi Host
$SetupIP = read-host "Enter Setup Room IP of Host"
$VMHost = Get-VMHost -name $SetupIP

##################################################################################

#Enabling vMotion TCP/IP Stack
$esxcli = Get-EsxCli -VMhost $VMHost -V2
$vMotion = @{
netstack = 'vmotion'
}
$esxcli.network.ip.netstack.add.Invoke($vMotion)

# Gets virtual switch0 on $VMHost
$VirtualSwitch = Get-VirtualSwitch -VMHost $VMHost -name vswitch0

# Making vMotion VMKernel Interfaces on vswitch0
New-VMHostNetworkAdapter -VMHost $VMHost -VirtualSwitch $VirtualSwitch -PortGroup S_vMotion1 -IP $S_vMotion1IP -SubnetMask $S_vMotion1Subnet -NetworkStack = "vmotion"
$VMHost | Get-VirtualPortGroup -Name S_VMotion1 | Set-VirtualPortGroup -VLanId 902

 

The below line is the one I am having the most issues with:

New-VMHostNetworkAdapter -VMHost $VMHost -VirtualSwitch $VirtualSwitch -PortGroup S_vMotion1 -IP $S_vMotion1IP -SubnetMask $S_vMotion1Subnet -NetworkStack = "vmotion"

If I take off -NetworkStack = "vmotion" it will successfully make a vmkernal interface but on the "Default" TCP/IP stack just like the Management VMKernal interface. Below is the error I receive when I leave -NetworkStack = "vmotion"

###############################################################################################

New-VMHostNetworkAdapter -VMHost $VMHost -VirtualSwitch $VirtualSwitch -PortGroup S_vMotion1 -IP $S_vMotion1IP -SubnetMask $S_vMotion1Subnet -NetworkStack = "vmotion"

New-VMHostNetworkAdapter : Cannot bind parameter 'NetworkStack'. Cannot convert the "=" value of type "System.String" to type "VMware.VimAutomation.ViCore.Types.V1.Host.Networking.HostNetworkStack".
At line:1 char:156
+ ... P $S_vMotion1IP -SubnetMask $S_vMotion1Subnet -NetworkStack = "vmotio ...
+ ~
+ CategoryInfo : InvalidArgument: (:) [New-VMHostNetworkAdapter], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,VMware.VimAutomation.ViCore.Cmdlets.Commands.Host.NewVMHostNetworkAdapter

###############################################################################################

I am not sure if it is a syntax error or if I am going about setting the "vMotion" TCP/IP stack incorrectly when creating the VMKernal interface. I will say I need this VMKernel interface to be add to the the vMotion TCP/IP stack, and not just have vMotion enabled under Enabled services for the VMKernel. 

Any input will be appreciated. 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That should be without the equal sign (-NetworkStack 'vmotion')


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Warren89
Contributor
Contributor
Jump to solution

@LucD Thank you for the quick response. Below is what I get when I take the = out:

 

New-VMHostNetworkAdapter -VMHost $VMHost -VirtualSwitch $VirtualSwitch -PortGroup S_vMotion1 -IP $S_vMotion1IP -SubnetMask $S_vMotion1Subnet -NetworkStack 'vmotion'

New-VMHostNetworkAdapter : Cannot bind parameter 'NetworkStack'. Cannot convert the "vmotion" value of type "System.String" to type "VMware.VimAutomation.ViCore.Types.V1.Host.Networking.HostNetworkStack".
At line:1 char:156
+ ... P $S_vMotion1IP -SubnetMask $S_vMotion1Subnet -NetworkStack 'vmotion'
+ ~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-VMHostNetworkAdapter], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,VMware.VimAutomation.ViCore.Cmdlets.Commands.Host.NewVMHostNetworkAdapter

Reply
0 Kudos
kwhornlcs
Enthusiast
Enthusiast
Jump to solution

I just converted my previous methods off ESXCLI methods and ran into a similar problem....Try adding in:

$vmtnStack = Get-VMHostNetworkStack -VMHost $VMHost | Where-Object {$_.Id -eq "vmotion"}
 
Then using your line as follows:
 
New-VMHostNetworkAdapter -VMHost $VMHost -VirtualSwitch $VirtualSwitch -PortGroup S_vMotion1 -IP $S_vMotion1IP -SubnetMask $S_vMotion1Subnet -NetworkStack $vmtnstack
 
 
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That means that the NetworkStack parameter doesn't support OBN (Object By Name).
Instead, try using the object returned by Get-VMHostNetworkStack, i.e.

 

 

$stack = Get-VMHostNetworkStack -VMHost $VMHost -Name 'vmotion'
New-VMHostNetworkAdapter -VMHost $VMHost -VirtualSwitch $VirtualSwitch -PortGroup 'S-vMotion1' -IP $S_vMotion1IP -SubnetMask $S-vMotion1Subnet -NetworkStack $stack

 

Updated: fixed the typo

 

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Warren89
Contributor
Contributor
Jump to solution

@LucD and everyone else on this tread thank you so much for your input on this. 

Reply
0 Kudos
Warren89
Contributor
Contributor
Jump to solution

@kwhornlcs  Would you be willing to share what you used to enable the vMotion TCP/IP stack via powershell and not esxcli?


For example I am using the following in order to enable the TCP/IP stack:
#Enabling vMotion TCP/IP Stack
$esxcli = Get-EsxCli -VMhost $VMHost -V2
$vMotion = @{netstack = 'vmotion'}
$esxcli.network.ip.netstack.add.Invoke($vMotion)

 

 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Afaik, the current PowerCLI version only allows "get"-ting and "set"-ting a network stack.

To create a new stack you will still need to use the 

$esxcli.network.ip.netstack.add

construct.

I suspect @kwhornlcs means that he replaced the 

$esxcli.network.ip.interface.add

 parts with the New-VMHostNetworkAdapter cmdlet.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
kwhornlcs
Enthusiast
Enthusiast
Jump to solution

The original esxcli methods I was using were correct with regards to the vmotion stack though they may have been out-dated - but it was this thread that got me to take a look at the esxcli methods I was using and simplify them all to the Get/Sets for VMHostNetworkAdapter and VMHostNetworkstack. (So big kudos to LucD as always for shining some light)

By default, the vmotion stack is present but I was finding the stack name is blank - so calling the stack by name wasn't feasible and the cmdlet doesn't allow a call by id - that's why I had to use where-object:

 

$vmtnstack = Get-VMHostNetworkstack -VMHost $vmhost | Where-Object {$_.ID -eq 'vmotion'}

 

Unlike when I used the esxcli method, I don't believe you need to specifically enable the vmotion stack, so I now toggle a boolean variable if any adapters are successfully added to the stack and use something like the following to set the stack name and enable routing on the vmotion stack:

 

If ($setstack) {
    $vmtnstack | Set-VMHostNetworkstack -Name 'vmotion' -VMKernelGateway 192.168.1.1
}

 

Where the gateway is specific to your vmotion vlan. 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Thanks for that, I wasn't aware the Name was blank.
In which vSphere version did you notice that?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

kwhornlcs
Enthusiast
Enthusiast
Jump to solution

I'm running version 7 update 3a - you know one of the recalled versions 😉 - but found the same to be true for other versions like 6.5 as is the case of an unaltered (no vmotion adapters) host below (specific host names hidden or omitted)

PS C:\Script> get-vmhostnetworkstack -VMHost xxxxxx.xxxxx.xxx|?{$_.id -eq 'vmotion'}|fl


Id                         : vmotion
Name                       : 
ExtensionData              : VMware.Vim.HostNetStackInstance
Uid                        : MaxNumberOfConnections     : 11000
CongestionControlAlgorithm : NewReno
IpV6Enabled                : True
Gateway                    : 
IpV6Gateway                : 
DnsDomainName              : 
DnsHostName                : 
DnsFromDhcp                : False
DnsAddress                 : 
DnsSearchDomain            : 

 

 

RobPhillipsFIS
Contributor
Contributor
Jump to solution

In V7 with Distributed switching - I have this for adding the VK and configuring the gateway on the stack itself - if a variable isn't explicitly defined below, the names should reflect what to set them to.

 

$Hostobject = get-vmhost hostname
$vmostack = Get-VMHostNetworkStack -VMHost $Hostobject | ? {$_.Id -eq "vmotion"}
New-VMHostNetworkAdapter -VirtualSwitch $toolsswitch -VMHost $Hostobject -PortGroup $VmoPortgroup -IP $esxihostvmoip -SubnetMask "255.255.255.0" -NetworkStack $vmostack
$vmostackconfig = New-Object VMware.Vim.HostNetworkConfig
$vmostackconfig.NetStackSpec = New-Object VMware.Vim.HostNetworkConfigNetStackSpec[] (1)
$vmostackconfig.NetStackSpec[0] = New-Object VMware.Vim.HostNetworkConfigNetStackSpec
$vmostackconfig.NetStackSpec[0].NetStackInstance = New-Object VMware.Vim.HostNetStackInstance
$vmostackconfig.NetStackSpec[0].NetStackInstance.RequestedMaxNumberOfConnections = 11000
$vmostackconfig.NetStackSpec[0].NetStackInstance.CongestionControlAlgorithm = 'newreno'
$vmostackconfig.NetStackSpec[0].NetStackInstance.IpRouteConfig = New-Object VMware.Vim.HostIpRouteConfig
$vmostackconfig.NetStackSpec[0].NetStackInstance.IpRouteConfig.DefaultGateway = $vmotiongateway
$vmostackconfig.NetStackSpec[0].NetStackInstance.Key = 'vmotion'
$vmostackconfig.NetStackSpec[0].Operation = 'edit'
$changeMode = 'modify'
$_this = Get-View -Id $Hostobject.NetworkInfo.Id
$_this.UpdateNetworkConfig($vmostackconfig, $changeMode)

Reply
0 Kudos
MikeEracleous
Contributor
Contributor
Jump to solution

Seems like this does not work on ESXi7? The part about getting the stack first. It claims the stack is not found. I did not need to get the stack first on ESXi6.x first though with ESXi 7 I appreciate you might have to but powercli refuses to see the vmo stack.

Reply
0 Kudos