Unneeded banter

So I was a bit lazy when I setup my home network the from the begining, I used the default vlan (1). So now this has haunted me for so long that I thought: It’s time to fix it.. So first this is still my private network. So I decided to use vlan 5 instead. So how do I make the transition easy for me and my networking equipment? Lets just loop vlan 1 and vlan 5. So a short ethernet cable later all machines on both vlan 1 and 5 can talk to each other.

Moving my virtual machines

So first of do I even have any virtual machines in my Hyper-V that is running on the default vlan? With the help of a simple Powershell query we shall soon find out.

Get-VM *|Get-VMNetworkAdapterVlan | ?{$_.accessvlanid -eq 1}
Lets break it down. First we get all virtual machines on the current host. Then we get all adapters on those virtual machines. And last a where AccessVlanID is 1. Simple as 1-2-3.

Okey so I have a few machines. Next lets just move them.

ForEach ($Vm in (Get-VM se.virot.wcs01))
{
  ForEach ($Nic in (Get-VMNetworkAdapter $vm))
  {
    $NicVLAN = Get-VMNetworkAdapterVlan -NetworkAdapter $Nic
    $NicVLAN | Where-Object {$_.accessvlanid -eq 1}| ForEach
    {
      Set-VMNetworkAdapterVlan -VlanId 5 -Access -VMNetworkAdapter $nic
    }
  }
}

Why so complicated you might think.. Well first we just make sure we find all interfaces that have the vlan of 1 set. First we have the same code as the just finding the machines. The final solution is a bit more complicated. It seems that I couldn’t find a link to the Network Adapter from the Vlan information, so I needed to first get the network interface and then get the vlan information.

If we run the first command again we will see that there are no more machines that need to be moved between the vlans.