Last week, we performed upgrades on our Hyper-V Server 2008 hosts to R2 that serve our Test\Dev environments. The upgrades were very straight forward and uneventful, which is what anyone hopes for when performing any upgrade. However, after the upgrade was complete, it appeared that either a virtual switch had either lost its configuration or failed. It turned out that it was one of those problematic Broadcom NIC’s that we all love so much that failed. So, I unbound the virtual switch that was configured to use the NIC from all VM’s and attempted to delete the switch. I was complimented with a nice error message, informing me that it couldn’t be deleted. So, I did what any good admin would do when the GUI fails us and turned to my trusty command line.
So, when administering Hyper-V using powershell, I use the Hyper-V library that is available from codeplex. Unfortunately, the powershell library doesn’t provide a cmdlet for deleting a virtual switch, so I leveraged a WMI class called “MSVM_VirtualSwitchManagementService”. Then, I used the “Choose-VMSwitch” powershell cmdlet to select the virtual switch for deletion and then deleted the switch. I searched online and found plenty of info on creating a virtual switch using powershell, but nothing regarding how to delete it, so I will walk you through deleting a virtual switch I created called “Demo Virtual Switch”.
In the steps that follow I will show you how to create the WMI object reference, select the virtual switch and finally deleted the selected switch. For this demo I am running the scripts local to the host with the virtual switch that I want to delete. With that said, let’s get started!
1. You need to start Powershell and set the execution policy to unrestricted to allow the scripts that follow to execute.
Set-ExecutionPolicy Unrestricted
2. Load the Hyper-V powershell library if it hasn’t already been done.
3. Now, like I said before, there isn’t a Powershell cmdlet for deleting a virtual switch. So, we will need to create an object reference to the WMI Class “MSVM_VirtualSwitchManagementService” so the switch can be deleted. Use the following syntax.
$HVSwitchObj = Get-WMIObject –class “MSVM_VirtualSwitchManagementService” –namespace “root\virtualization” –computer “.”
4. Now, we need to select the virtual switch that we want to delete. There is a powershell cmdlet called Choose-VMSwitch that will allow us to make this selection. The following syntax will provide a list of all the virtual switches that exist on the host and provide the ability to select the switch that is to be deleted. Enter your switch selection by typing the associated ID number from the list and hit enter.
$HVSwitch = Choose-VMSwitch
5. Finally, we delete the switch. The final syntax uses the WMI class to delete the virtual switch selected by the powershell cmdlet.
$HVSwitchObj.DeleteSwitch($HVSwitch)
If you now pull up "Hyper-V Manager" and select “Virtual Network Manager”, you will see that the virtual switch has been removed and problem resolved. At this point I was able to replace my failed NIC, create a new virtual switch and bind the new switch to my virtual machines.
I hope this helps!
