VMware PowerCLI

The Best User Interface for your VMware Datacenter!

Set-NetworkAdapter does not support attaching VMs to NSX-T Logical Switches

Currently, the Set-NetworkAdapter cmdlet is not aware of NSX-T Logical Switches which are backed by the OpaqueNetwork network type. This prevents customers from being able to easily change or attach a VM to an NSX-T Logical Switch and would have to resort to the "Views" to be able to reconfigure the VM. 

The following snippet was recently shared from few NSBU folks and it would be great if we can extend support for NSX-T Logical Switches

$vm = Get-VM
$adapter = $vm | Get-NetworkAdapter
$opaqueNetwork = Get-View -ViewType OpaqueNetwork
$opaqueNetworkBacking = New-Object VMware.Vim.VirtualEthernetCardOpaqueNetworkBackingInfo
$opaqueNetworkBacking.OpaqueNetworkId = $opaqueNetwork.Summary.OpaqueNetworkId
$opaqueNetworkBacking.OpaqueNetworkType = $opaqueNetwork.Summary.OpaqueNetworkType
$device = $adapter.ExtensionData
$device.Backing = $opaqueNetworkBacking
$spec = New-Object VMware.Vim.VirtualDeviceConfigSpec
$spec.Operation = [VMware.Vim.VirtualDeviceConfigSpecOperation]::edit
$spec.Device = $device
$configSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
$configSpec.DeviceChange = @($spec)
$vm.ExtensionData.ReconfigVM($configSpec)
  • William Lam
  • Jun 8 2018
  • Shipped
  • Attach files
  • Yogesh Dabas commented
    13 May 10:42


    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(rc_lgs_ow is the name of logical switch):

    PS C:\Users\ydabas> Get-VM rc_geneve_o | New-NetworkAdapter  -NetworkName "VM Network" | Set-NetworkAdapterOpaqueNetwork -OpaqueNetworkName "rc_lgs_ow" -StartConnected:$true

    Name                 Type       NetworkName  MacAddress         WakeOnLan
                                                                      Enabled
    ----                 ----       -----------  ----------         ---------
    Network adapter 3    Vmxnet3    rc_lgs_ow    00:50:56:af:b3:ad      False