Identify Hyper-V VM network adapter connected to specific vSwitch

2

I am trying to identify a specific network adapter connected to the Internal vSwitch, so I can subsequently bind the DHCP server role to that adapter. There are 3 Network Adapters, 2 connected to External (connected to my laptops Wifi & Ethernet adapter) vSwitches, and 1 to Internal (shared only between VM's). However they are not easily identified from within the VM.

Name                      InterfaceDescription                    ifIndex Status       MacAddress             LinkSpeed
----                      --------------------                    ------- ------       ----------             ---------
Ethernet 2                Microsoft Hyper-V Network Adapter #2          6 Up           00-15-5D-01-64-16       270 Mbps
Ethernet 3                Microsoft Hyper-V Network Adapter #3          4 Up           00-15-5D-01-64-17        10 Gbps
Ethernet                  Microsoft Hyper-V Network Adapter             5 Disconnected 00-15-5D-01-64-15        10 Gbps

The state of either external adapter can be connected or not, but usually 1 is (Wifi most likely). In above example its probably the "Ethernet 3" adapter, but thats only because my Ethernet isn't connected. It will have to work in a PowerShell script (PSSession). The VM is running Server 2016. How to identify the internal adapter to bind DHCP to?

Thanks a lot!

Edit: I could also wait with creating the internal network adapter until ready to bind dhcp to it and store the previously existing adapters first before creating the internal adapter.

SuHwak

Posted 2016-01-01T14:47:15.027

Reputation: 55

Answers

2

I know this question is outdated, but recently I've created a PowerShell script to solve this problem. May be it will be useful to someone else.

$net_adapters = Get-NetAdapter
foreach($ethernet_port in gwmi -Namespace Root\Virtualization\V2 -Class Msvm_InternalEthernetPort){
    $endpoint_physical = gwmi -Namespace Root\Virtualization\V2 -Query "ASSOCIATORS OF {$ethernet_port} WHERE ResultClass=Msvm_LANEndpoint AssocClass=Msvm_EthernetDeviceSAPImplementation"
    $endpoint_virtual = gwmi -Namespace Root\Virtualization\V2 -Query "ASSOCIATORS OF {$endpoint_physical} where ResultClass=Msvm_LANEndpoint assocclass=Msvm_ActiveConnection"
    $ethernetswitchport = gwmi -Namespace Root\Virtualization\V2 -Query "ASSOCIATORS OF {$endpoint_virtual}  WHERE ResultClass=Msvm_EthernetSwitchPort AssocClass=Msvm_EthernetDeviceSAPImplementation"
    $vswitch = gwmi -Namespace Root\Virtualization\V2 -Query "ASSOCIATORS OF {$ethernetswitchport} WHERE ResultClass=Msvm_VirtualEthernetSwitch"

    $net_adapter = $net_adapters | ?{($_).MacAddress -replace '-','' -eq $ethernet_port.PermanentAddress}
    Write-Host "Adapter:" $net_adapter.Name 
    Write-Host "Switch:" $vswitch.ElementName
    Write-Host
}

Dmitry Trukhanov

Posted 2016-01-01T14:47:15.027

Reputation: 136