Hyper-V - How to set VLANs for specyfied network adapter on VM?

Hello,

I have network switches with VLANs. I was able to set VLAN settings for switches by using this:

Set-VMNetworkAdapterVlan -VMNetworkAdapterName NAME -Access -VlanID 100 -ManagementOS

Now, I have to set VLANs specified VM - How to do this by Powershell?

In case filtering by VMNAME + VMADAPTER -> Set-VMNetworkAdapterVlan -VMName NAME -VMNetworkAdapterName NAME -Access -VlanId 100

I can’t filter because i have three adapters on VM and this query is not able to select correct because display name is always “nework adapter”

Then change the display name, or is there a reason that you set those the same?
Even if you did, use the adapter ID or ifindex.

Get-NetAdapter
Set-NetAdapter Sets the basic network adapter properties. https://docs.microsoft.com/en-us/powershell/module/netadapter/set-netadapter?view=win10-ps

Description
The Set-NetAdapter cmdlet sets the basic properties of a network adapter such as virtual LAN (VLAN) identifier (ID) and MAC address. These changes could disrupt proper networking functionality. Other network adapter properties may be set by using a cmdlet such as the Set-NetAdapterRss, Set-NetAdapterLso, or Set-NetAdapterAdvancedProperty cmdlets.

Examples
Example 1: Set the specified network adapter to a different VLAN ID
PowerShell

Set-NetAdapter -Name "Ethernet 1" -VlanID 10

I can’t use Set-NetworkAdapter because it is setting properties for network adapters on Hyper-V host. I have to set those settings for adapters which are in VM. My VM has three adapters and those adapters should have different VLANs:

Get-VMNetworkAdapterVlan -VMName VM1
Result:

VMName VMNetworkAdapterName Mode VlanList


VM1 Network Adapter Access 100
VM1 Network Adapter Access 200
VM1 Network Adapter Access 300

If Iuse this to set values: Set-VMNetworkAdapterVlan -VMName VM1 -Access -VlanId 100

then it set VLAN 100 for all adapters:

VMName VMNetworkAdapterName Mode VlanList


VM1 Network Adapter Access 100
VM1 Network Adapter Access 100
VM1 Network Adapter Access 100

and this is my goal but I have to have different VLANs for those adapters. How to do this? Maybe is there any index or something? NetworkAdapterName is the same for all adapters. So I can’t use this:

Set-VMNetworkAdapterVlan -VMName VM1 -VMNetworkAdapterName AdapterName -Access -VlanId 100

 

I can try to rename it but how to select one adapter instead of all if this has the same name:

 

Rename-VMNetworkAdapter -VMName VM1 -Name “Network Adapter” -NewName “Adapter”

Get-VMNetworkAdapterVlan -VMName VM1

VMName VMNetworkAdapterName Mode VlanList


VM1 Adapter Access 100
VM1 Adapter Access 100
VM1 Adapter Access 100

How to distinguish adapters?

Any ideas or maybe I’m doing something wrong?

Remember in my first reply, regardless of what you name an adapter, the most unique item of an adapter is its, adapter ID or ifindex or Mac address.

Clear-Host
Get-WmiObject win32_networkAdapterConfiguration -ComputerName $env:COMPUTERNAME | 
Where IPAddress -ne $null | 
Select-Object -Property Index, InterfaceIndex, IPAddress, MACAddress, Description | 
Sort-Object -Property Index
Format-List -Force

So, you are running these commands from an admin workstation. Yet, you have in the Hyper-V Manager multiple switch definitions and on each switch you have the…

<strong>VLAN ID --- Enable virtual LAN identification for management operating system</strong><em>

… checkbox, checked, and you have each adapter on one switch set to VLAN 100,200, 300 and you have these three adapters assigned to VM1?

Hello,

Yes, I have all three adapters assigned to VM and those adapters need have different VLANs and I have to configure it from a command line.

It looks like here: http://prntscr.com/mx78w9

I have to configure VM Adapters and set different VLANs for it. When I run your command you can see that I can see and configure adapters on Host machine for that VM. I need to do those settings on VM.

I can use it but it set the same VLAN for all network adapter in my VM: Set-VMNetworkAdapterVlan -VMName VM1 -VMNetworkAdapterName AdapterName -Access -VlanId 100

I don’t know how to distinguish it.

 

I still wasn’t able to find a solution - Am I doing something wrong? It should be obvious configuration for Powershell and Hyper-V

Up:) Maybe someone can help - I still don’t have a solution. I’ve provided more details above.