Repair Hyper-V hosts and virtual machines with PowerShell

Although Hyper-V is generally a very reliable platform for hosting virtual machines, things can and sometimes do go wrong. When this happens, there are several different PowerShell cmdlets available to repair Hyper-V hosts and fix the problem.

Before I get started, I need to point out that the PowerShell cmdlets that I am going to be talking about are not native to Hyper-V. Instead, they are included with System Center Virtual Machine Manager and are accessible only through the Virtual Machine Manager Command Shell.

Finding the problem

The cmdlet used to repair Hyper-V hosts is Get-SCVMHost. According to Microsoft, the command “starts a set of remediation steps on a host in a failed state for a known set of failure conditions.” In other words, the cmdlet checks to see if the failure of a host can be attributed to a known cause, and then works to fix the problem.

As is so often the case when working in a System Center Virtual Machine Manager environment, you will need to leverage a RunAs account when repairing the host. Therefore, the first thing that you will need to do is to map a RunAs account to a variable. The command below for example, maps a RunAs account named RunAs to a variable named $RunAsAccount:

$RunAsAccount = Get-SCRunAsAccount -Name “RunAs”

Once you have mapped the RunAs account to a variable, you can launch the repair operation. Here is an example of a command used for doing so:

Get-SCVMHost -ComputerName “<Host Server Name>” | Repair-SCVMHost -Credential $RunAsAccount

You can see an example of this process in the screenshot below.

Fixing an individual virtual machine problem

Just as you can use PowerShell to remediate a Hyper-V host problem, you can also use a similar technique to fix problems that have occurred on individual virtual machines. There are four main types of failures that this technique is able to correct. These include creation failures, migration failures, update failures, and deletion failures. In addition, the Repair-SCVirtualMachine cmdlet can be used to fix problems with a System Center agent that has been installed within the guest operating system.

Repairing these problems involves the use of the Repair-SCVirtualMachine cmdlet. Before I show you how to use this cmdlet, I need to spend a moment talking about the actions.

Whenever you use the Repair-SCVirtualMachine cmdlet, you will need to specify one of three actions. The first of these actions is Retry. This action is useful when a virtual machine is in a failed state because of some administrative action that you performed, and you simply want to retry the action. To put this more in prospective, I actually used this action last night. I was performing an administrative task related to storage provisioning. The task timed out due to slow hardware, but I used the Retry action, and the task completed the second time around.

The second action that you can perform is Undo. Undo is useful for reverting a virtual machine to its previous state following a failed administrative action. If for example, you attempt to migrate a virtual machine to a new host, but the migration operation fails, you can use the Undo action to move the VM back to its previous host.

The third action is Dismiss. You would only use the Dismiss action in situations in which you have manually corrected a problem, and want to get rid of the virtual machine’s failed state. Using this option essentially just performs a refresh operation.

So let’s take a look at a couple of examples of how you might repair a virtual machine. Let’s suppose that you attempted to migrate a VM named MyVM to a new host, but the operation has failed and you want to undo it. This would cause the virtual machine to be re-homed back onto the host that had originally been hosting it. Performing this type of corrective action involves the use of two commands:

$VM = Get-SCVirtualMachine -Name “MyVM”
Repair-SCVirtualMachine -VM $VM -Undo

The first command maps a variable named $VM to the specified virtual machine. In this case, that virtual machine is named MyVM.

The second command is the one that actually repairs the virtual machine. As you can see, the -VM switch is used to point the cmdlet to the virtual machine that needs to be fixed, and the repair action is specified through the -Undo switch. If I wanted to retry the operation instead, I would replace -Undo with -Retry.

What about a guest operating system?

So what about a situation in which there are problems with a System Center agent that has been installed within a VM’s guest operating system? In this type of situation, things work just a little bit differently than the last example that I showed you. You would still need to map a variable to the virtual machine as was done before, but the rest of the process works differently. Here are the PowerShell commands:

$VM = Get-SCVirtualMachine -Name “MyVM”
$Cred = Get-Credential
Repair-SCVirtualMachine -VM $VM -Credential $Cred -Agent

So as you can see, the first line is identical to the first line used in the previous example. The second command creates a variable named $Cred and captures a set of credentials within that variable. These credentials are used to log into the virtual machine that is having problems.

The third command is the one that actually initiates the repair operation. The Repair-SCVirtualMachine cmdlet is used in conjunction with the -VM switch as before, but the -Credential switch is also used. This allows the Repair-SCViretualMachine cmdlet to use the virtual machine’s credentials. The other thing that is worth pointing out is that because we are fixing a problem with a virtual machine’s agent, an action (undo, retry, etc.) is not used. Instead, the Agent switch instructs the cmdlet to perform an agent level repair.

Other ways to repair Hyper-V hosts

As useful as it may be to be able to repair Hyper-V hosts and virtual machines from the command line, these are not the only repair options that are available. Microsoft also provides a Repair-SCVirtualNetwork cmdlet and a Repair-SCVirtualNetworkAdapter cmdlet that can be used to repair virtual networks and virtual network adapters respectively.

Featured image: Shutterstock

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

Recent Posts

Google faces probe for GDPR data protection violations

In what may be a landmark case, Google faces a hefty fine if it is found guilty of violating GDPR…

15 hours ago

IT security practices that have stood the test of time

Sometimes, old wisdom is the best wisdom. Protect your data with these IT security best practices that have proven effective…

19 hours ago

Xtreme Podcast: Has Big Data been overshadowed by dark data?

Today’s Xtreme Podcast: Shining a light on dark data, legacy security tools, and hacking stats that will leave you more…

21 hours ago

Cryptojacking: Don't let your system perform for someone else

In most cyberattacks, hackers want you to know you’ve been compromised. But in cryptojacking, hackers want you to live in…

2 days ago

System feeling down? Architect your enterprise apps for high availability

Businesses want to improve uptime, and optimizing every part of their technology stack for high availability is a significant step…

2 days ago

10 hacking stats every business leader and IT pro must know

Cybercrime is bad and getting worse. Yes, these 10 hacking stats will scare you, but knowing about them can help…

2 days ago