It seems using Opaque networks has got a bit easier in a recent release of PowerCLI.
NSX-T Logical Switches are seen as opaque
networks by a vSphere host. This means that traditionally you had to modify the view of the object like below to “attach” a VM to a logical switch
########### # Collecting Opaque networks $adapter1 = $vm | Get-NetworkAdapter -name "Network Adapter 1" # This will target the opaque network to connect to. $opaqueNetwork1 = Get-View -ViewType OpaqueNetwork | ? {$_.name -eq $k8spodlsname} # Create an opaque network backing PSObject $opaqueNetworkBacking1 = New-Object VMware.Vim.VirtualEthernetCardOpaqueNetworkBackingInfo # Set the OpaqueNetworkId and OpaqueNetworkType to the ones specified by logical switch to connect to $opaqueNetworkBacking1.OpaqueNetworkId = $opaqueNetwork1.Summary.OpaqueNetworkId $opaqueNetworkBacking1.OpaqueNetworkType = $opaqueNetwork1.Summary.OpaqueNetworkType # Modify the device object stored locally $device1 = $adapter1.ExtensionData $device1.Backing = $opaqueNetworkBacking1 # Create a new "edit" device config spec $spec1 = New-Object VMware.Vim.VirtualDeviceConfigSpec $spec1.Operation = [VMware.Vim.VirtualDeviceConfigSpecOperation]::edit $spec1.Device = $device1 # Create a new config spec for the VM and add the device edit config spec from above $configSpec1 = New-Object VMware.Vim.VirtualMachineConfigSpec $configSpec1.DeviceChange = @($spec1) # Reconfigure the VM $vm.ExtensionData.ReconfigVM($configSpec1)
With the latest release it has become far more easier to attach a Logical Switch (VLAN or Overlay) to a network adapter.
$null = $vm2 | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName $ls2.display_name -confirm:$false
This takes the display_name
value from the variable $ls2
and uses it for the -NetworkName
property. Much easier.
For completeness here is how to create the Logical Switch
$transportZone = $serviceTransportZones.list().results | Where-Object {$_.display_name -eq $transportZoneName} $specLogicalSwitch = $serviceLogicalSwitch.help.create.logical_switch.Create() $specLogicalSwitch.display_name = $ls1name $specLogicalSwitch.transport_zone_id = $transportZone.id $specLogicalSwitch.admin_state = "UP" $specLogicalSwitch.replication_mode = "MTEP" $specLogicalSwitch.tags.Add($DemoTags) | Out-Null $ls1 = $serviceLogicalSwitch.create($specLogicalSwitch)
Here is my version of PowerCLI/PowerShell for your reference
PS /Users/aburke/Repositories/nsx-scripts> $psversiontable Name Value ---- ----- PSVersion 6.1.0 PSEdition Core GitCommitId 6.1.0 OS Darwin 18.0.0 Darwin Kernel Version 18.0.0: W... Platform Unix PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...} PSRemotingProtocolVersion 2.3 SerializationVersion 1.1.0.1 WSManStackVersion 3.0
Happy days!
Thanks for this. Sadly it looks like $vm | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName $opaquenetworkname -confirm:$false is not working with PowerCLI v11 targeting a VCSA v6.5U1 using PowerShell v5.1. The Get-View configspec method saved the day!