Configuring NSX-T opaque network as a VM port group with PowerCLI
search cancel

Configuring NSX-T opaque network as a VM port group with PowerCLI

book

Article ID: 324798

calendar_today

Updated On:

Products

VMware NSX VMware vSphere ESXi

Issue/Introduction

Symptoms:
Managing a virtual machine’s network configuration specific to port groups being sourced from NSX-T based opaque networks may fail because the network is unable to be found on the host.

Command Example:
Get-VM "APP-VM1" | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName “network-name”

Error Example:
Set-NetworkAdapter : 22/03/2018 15:56:17 Set-NetworkAdapter The network "network-name" doesn't exist on the host.
At line:1 char:38
+ ... rkAdapter | Set-NetworkAdapter -NetworkName “network-name”
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : ResourceUnavailable: (Network-name:String) [Set-NetworkAdapter], iError
    + FullyQualifiedErrorId : Client20_VmHostServiceImpl_TryGetHostNetworkByName_NonexistentNetwork,VMware.VimAutomation.ViCore.Cmdlets.Commands.VirtualDevice.SetNetworkAdapter


Environment

VMware NSX-T

Resolution

Currently, there is no resolution.

Workaround:
To work around this issue, a new object for the opaque network has to be created to properly configure the port group setting on a virtual machine.

The function Set-NetworkAdapterOpaqueNetwork will create that new object and will replace the usage of Set-NetworkAdapter in the meantime.

function Set-NetworkAdapterOpaqueNetwork {
param(
    [Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 1)]
    [VMware.VimAutomation.Types.NetworkAdapter]
    $NetworkAdapter,

    [Parameter(Mandatory = $true, Position = 2)]
    [string]
    $OpaqueNetworkName,

    [Parameter()]
    [switch]
    $Connected,

    [Parameter()]
    [switch]
    $StartConnected
)
process {
    $opaqueNetwork = Get-View -ViewType OpaqueNetwork | ? {$_.Name -eq $OpaqueNetworkName}
    if (-not $opaqueNetwork) {
        throw "'$OpaqueNetworkName' network not found."
    }

    $opaqueNetworkBacking = New-Object VMware.Vim.VirtualEthernetCardOpaqueNetworkBackingInfo
    $opaqueNetworkBacking.OpaqueNetworkId = $opaqueNetwork.Summary.OpaqueNetworkId
    $opaqueNetworkBacking.OpaqueNetworkType = $opaqueNetwork.Summary.OpaqueNetworkType

    $device = $NetworkAdapter.ExtensionData
    $device.Backing = $opaqueNetworkBacking

    if ($StartConnected) {
        $device.Connectable.StartConnected = $true
    }

    if ($Connected) {
        $device.Connectable.Connected = $true
    }
    
    $spec = New-Object VMware.Vim.VirtualDeviceConfigSpec
    $spec.Operation = [VMware.Vim.VirtualDeviceConfigSpecOperation]::edit
    $spec.Device = $device
    $configSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
    $configSpec.DeviceChange = @($spec)
    $NetworkAdapter.Parent.ExtensionData.ReconfigVM($configSpec)

    # Output
    Get-NetworkAdapter -Id $NetworkAdapter.Id
    }
}


Command Example:

Get-VM "APP-VM1" | Get-NetworkAdapter | Set-NetworkAdapterOpaqueNetwork -OpaqueNetworkName "network-name"