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", `
, `
$null, `
$null, `
, `
, `
$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(`
) `
| 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", `
, `
$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(`
) | 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

Comments (4)

  1. Nipun says:

    Hi Taylor,

    I have asked this question somewhere else also on the blog, but let me repeat it here as I see this is more appropriate place.

    I want to know some wmi class which can give me the details of network adapters on vm with a mapping between ip and mac address.

    So in effect I want to know wmi class which I can query on the hyperV host and this class will return me the mapping of ip address and mac address of network adpaters of virtual machine hosted by hyperV.

    I know there are powershell commands but I just need the wmi classes.



  2. Behradz says:


    I could use your code to plug my card to a switch but it works only if the card has never been connected to a switch.  You cannot use it to unplug and plug to any other card.

    I think we need to RemoveResourceSettings but couldn't figure out how. Can you help please?


  3. Ionut says:



    Did you succeed to do that?

    I am trying to do that for almost two weeks.

    Thank you.

  4. Kevin says:

    Behradz or Ionut,

    Any luck?  I realize this is 18 months old, but I'm trying at the exact same spot!