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.

Problem 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.

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.


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

Brien Posey

Brien Posey is a freelance technology author and speaker with over two decades of IT experience. Prior to going freelance, Brien was a CIO for a national chain of hospitals and healthcare facilities. He has also served as a network engineer for the United States Department of Defense at Fort Knox. In addition, Brien has worked as a network administrator for some of the largest insurance companies in America. To date, Brien has received Microsoft’s MVP award numerous times in categories including Windows Server, IIS, Exchange Server, and File Systems / Storage. You can visit Brien’s Website at: www.brienposey.com.

Share
Published by
Brien Posey
Tags Powershell

Recent Posts

Losing your edge? 7 free tools to keep you focused at work

Staying focused at work in an always-connected world is hard! Here’s how to use tech — and some free tools…

16 hours ago

What’s next in the evolution of biometrics and facial recognition technology?

Facial recognition technology has matured to the point of being reliable — for better or for worse. What does the…

20 hours ago

Locking down your Exchange server with cipher suites

Cipher suites are a set of algorithms you need to secure your environment, either by using SSL and TLS. Here’s…

23 hours ago

AI cyber risks: What to look out for when deploying AI technology

Artificial intelligence has greatly improved modern life. But businesses must recognize that AI cyber risks exist and take appropriate measures.

2 days ago

Review: Office 365 synchronizing and administration tool CiraSync

CiraSync offers an enterprise solution for syncing global address list contacts and calendars to smartphones and other mobile devices. Here’s…

2 days ago

HIPAA IT compliance: Privacy and security rules you must know

HIPAA is the mandatory health regulation that must be followed strictly. But if you’re an IT pro in the health-care…

2 days ago