Connecting a VM Network Adapter To A Switch Using The Hyper-V WMI V2 Namespace

Following along with Adding a Network Adapter To A VM Using The Hyper-V WMI V2 Namespace you might want to connect a network adapter to a switch…  Here’s how you would that, one thing to comment on is that the object returned when calling AddResourceSettings from the previous example is the Msvm_SyntheticEthernetPortSettingData class – which means you can skip about the first half of this code…

$vmName = "Test"
$switchName = "Demo"

#Retrieve the Hyper-V Management Service, ComputerSystem class for the VM and the VM’s SettingData class.
$Msvm_VirtualSystemManagementService = Get-WmiObject -Namespace root\virtualization\v2 ` 
      -Class Msvm_VirtualSystemManagementService

$Msvm_ComputerSystem = Get-WmiObject -Namespace root\virtualization\v2 `
      -Class Msvm_ComputerSystem -Filter "ElementName='$vmName'"

$Msvm_VirtualSystemSettingData = ($Msvm_ComputerSystem.GetRelated("Msvm_VirtualSystemSettingData", `
"Msvm_SettingsDefineState", `
      $null, `
      $null, `
"SettingData", `
"ManagedElement", `
      $false, $null) | % {$_})

#Retrieve the VirtualSwitch class the NIC will Connect to
$Msvm_VirtualEthernetSwitch = Get-WmiObject -Namespace root\virtualization\v2 `
      -Class Msvm_VirtualEthernetSwitch -Filter "ElementName='$switchName'"

#Retrieve the NetworkAdapterPortSettings Associated to the VM.
$Msvm_SyntheticEthernetPortSettingData = ($Msvm_VirtualSystemSettingData.GetRelated(`
"Msvm_SyntheticEthernetPortSettingData") `
      | Where-Object {$_.ElementName -eq "Network Adapter"})

#Retrieve the default (primordial) resource pool for the Ethernet Connection
$Msvm_ResourcePool = (Get-WmiObject -Namespace root\virtualization\v2 -Class Msvm_ResourcePool `
      -Filter "ResourceSubType = 'Microsoft:Hyper-V:Ethernet Connection' and Primordial = True" | % {$_})

#Retrieve the AllocationCapabilities class for the Resource Pool
$Msvm_AllocationCapabilities = ($Msvm_ResourcePool.GetRelated("Msvm_AllocationCapabilities", `
"Msvm_ElementCapabilities", `
      $null, `
      $null, `
      $null, `
      $null, `
      $false, `
      $null) | % {$_})

#Query the relationships on the AllocationCapabilities class and find the default class (ValueRole = 0)
$Msvm_SettingsDefineCapabilities = ($Msvm_AllocationCapabilities.GetRelationships(`
"Msvm_SettingsDefineCapabilities") | Where-Object {$_.ValueRole -eq "0"})

#The PartComponent is the Default SyntheticEthernetPortSettingData class values
$Msvm_EthernetPortAllocationSettingData = [WMI]$Msvm_SettingsDefineCapabilities.PartComponent

#Specify the NIC's Port Setting and the Switch Path
$Msvm_EthernetPortAllocationSettingData.Parent = $Msvm_SyntheticEthernetPortSettingData
$Msvm_EthernetPortAllocationSettingData.HostResource = $Msvm_VirtualEthernetSwitch

#Add the connection object which connects the NIC
$Msvm_VirtualSystemManagementService.AddResourceSettings($Msvm_VirtualSystemSettingData, $Msvm_EthernetPortAllocationSettingData.GetText(2))

-Taylor Brown
-Program Manager, Hyper-V