Fixing problems with Hyper-V virtual networks using PowerShell

Microsoft Hyper-V has been around for over a decade now, and as such, it has had plenty of time to mature. For the most part, Hyper-V is a very stable and reliable enterprise-class hypervisor. Even so, things can, and sometimes do go wrong. About four or five months ago for example, I ran into a situation in which all of the virtual machines on a particular host suddenly lost the ability to access the Internet. To this day, I can’t explain what happened, because no configuration changes were being made. The point is that although you might not be able to stop a Hyper-V virtual network problem from occurring, it is a good idea to know what your options are for fixing any problems that do occur. In this article, I will show you one of the tools that I like to use.

Lately, I have been using a PowerShell-based tool to fix problems with Hyper-V virtual networks. Before I show you how this tool works, I need to point out that it is not included in native PowerShell. The tool is actually a part of System Center Virtual Machine Manager, and is designed to be run through the Virtual Machine Manager Command Shell.

So what is this tool? The associated cmdlet is Repair-SCVirtualNetwork.

Using the PowerShell cmdlet

Using the Repair-SCVirtualNetwork cmdlet is usually pretty simple and straightforward. You will usually just have to identify the virtual network that you want to repair. What can make this a bit tricky, however, is that it is common for multiple Hyper-V hosts to use a common virtual switch name.

Let’s pretend for a moment that I have two Hyper-V hosts named Hyper-V-1 and Hyper-V-2. Let’s also pretend that I have a virtual machine named VM1 running on Hyper-V-1, and that I want to live migrate that VM to Hyper-V-2. If that live migration is to be completely seamless, then Hyper-V-2 will need to be provisioned with a virtual switch that has the same name as the virtual switch that VM1 is currently using on Hyper-V-1. The end result is that there will be multiple virtual switches with the same name.

If you want to see a more concrete example, then check out the screen capture below. In this image, I have entered the Get-SCVirtualNetwork cmdlet. I then used the Select-Object cmdlet to specify that I wanted to see the virtual network name, and the name of the Hyper-V host on which the virtual network’s virtual switch resides. As you can see in the figure, I have several Hyper-V hosts that contain a virtual network named My Virtual Switch.

Virtual NetworksProblem and workaround

Unfortunately, the Repair-SCVirtualNetwork PowerShell cmdlet does not contain a parameter that allows you to specify that a repair should be made against a specific host. The cmdlet only allows you to supply the name of the virtual network that you want to repair. So how do you get around the problem of having identical virtual network names on multiple hosts?

Before I show you how to solve this problem, I want to show you that in some convoluted way, System Center collectively treats these identically named virtual switches as if they were a single virtual network, even though they are actually separate entities. Let me show you what I mean.

If you open the Virtual Machine Manager console, go to the VMs and Services workspace, and then click on VM Networks, you will see a list of the VM networks that exist within your organization. If you look at the figure below, you will notice that the console only displays a single instance of My Virtual Switch, even though the All Hosts group clearly contains each of the hosts that were shown in the previous screen capture.

virtual networks

Additionally, if I were to right click on the virtual switch, and choose the View Dependent Resources command from the shortcut menu, I would see a list of all of the virtual machines that depend on this virtual switch. These virtual machines are distributed across the hosts in my organization. Incidentally, my Hyper-V hosts are not clustered.

Since there are multiple instances of the virtual network in question, the first thing that needs to be done prior to repairing the virtual network is to map those virtual network instances to a variable. By doing so, I can narrow things down so that I only reference a single virtual network. This is important, because the Repair-SCVirtualNetwork PowerShell cmdlet will fail if you try to run it against multiple virtual networks at once.

Normally, this is the way that you would map a virtual network to a variable:

$VirtualNetwork = Get-SCVirtualNetwork -Name “My Virtual Switch”

However, since the goal is to capture a single virtual network, we need to append a filter. That way, we can specify which host’s virtual network we want to examine. Here is what the command looks like:

$VirtualNetwork = Get-SCVirtualNetwork -Name “My Virtual Switch” | Where-Object {$_.VMHost -eq “hyper-v-1.mgmt.com”}

If you look at the screenshot below, you can see that the variable that I have defined points to a single virtual network, on a single host.

powershell
With the virtual networks mapped to a variable, we can launch the repair process. As previously mentioned, all you have to do is run the Repair-SCVirtualNetwork cmdlet, and append the name of the virtual network that you want to repair. Since the virtual network instance has been mapped to a variable in this case though, we need to reference the variable rather than providing an actual virtual network name. Here is what the command would look like:

Repair-SCVirtualNetwork -VirtualNetwork $VirtualNetwork

Hyper-V virtual network problems: More information

Thankfully, Hyper-V virtual network problems tend to be somewhat rare occurrences. When problems do occur, though, you may be able to use the Repair-SCVirtualNetwork cmdlet to fix the issue. You can find the cmdlet’s full documentation here.

Featured image: Pixabay

About The Author

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top