Sunday, April 5, 2020

Replace HyperV virtual switch for VMs with Powershell

Replace HyperV virtual switch for VMs with Powershell


In this post, I will show you how to replace virtual switch for VMs in an easy way with Powershell.

Recently I had to remove existing switch for all Virtual machines (~ 50) on one HyperV host. One of the reason why you would do that is to enable "SR-IOV" as you can't do that after you create switch.

All Vms had two network adapters with internal and public switches. In this case, we had to reconfigure those network adapters that use Public switch.

In order to accomplish this, you have to follow these steps:

1. Create new temporary switch that will replace existing public switch




2. Replace existing public switch with the new temp switch using these Powershell commands


$vms = get-vm  

foreach ($vm in $vms){
Get-VMNetworkAdapter -VMName $vm.name | where switchname -eq 'public' |
Connect-VMNetworkAdapter -SwitchName 'tempswitch'

}



3. Check if new switch is properly applied and remove 'old' public switch

4. Create new Public switch that will replace temp switch



4. Replace temp switch with a new Public switch using these Powershell commands

$vms = get-vm 

foreach ($vm in $vms){
Get-VMNetworkAdapter -VMName $vm.name | where switchname -eq 'tempswitch' |
Connect-VMNetworkAdapter -SwitchName 'public'

}


No comments:

Post a Comment