Posey's Tips & Tricks
DIY Network Monitoring for Hyper-V, Part 1
A failed Hyper-V replication setup prompts a PowerShell-based approach to building lightweight, do-it-yourself health monitoring without the cost or complexity of an enterprise-grade tool
As a freelancer, I am completely dependent on a handful of virtual machines that I use to run my business. Because I cannot function without these virtual machines, I use Hyper-V replication to ensure business continuity. More specifically, I have a duplicate Hyper-V server and a duplicate storage array and I use the Hyper-V replication feature to maintain synchronized replicas of my virtual machines on redundant hardware. That way, if anything ever happens to my primary environment, I can easily fail over to the backup hardware and continue running my mission critical workloads.
I have been using this architecture for years and it has always worked well. The problem however, is that I have gotten just a little bit too comfortable with how well everything works and have just been assuming that everything is working normally without really checking.
Unfortunately, I recently discovered that my production environment suffered a catastrophic replication failure months ago and because I never bother to look at my logs, I didn’t even notice the failure until now.
Thankfully, I was never put into the position of needing to recover from a disaster, only to find that my recovery environment was useless. Instead, I stumbled onto the problem while working on something unrelated. Needless to say, I promptly fixed the issue.
Even though I found and fixed the problem before anything bad happened, it occurred to me that if this were to happen again at some point in the future, I might not be so lucky next time. Since I have neither the time nor the self-discipline to check my log files every day, I knew that I needed to come up with some sort of alerting mechanism that would let me know when problems occur. At the same time though, I really didn’t want to invest in an enterprise grade network monitoring solution. Most are as complicated as they are pricey. I decided that the best solution would be to use PowerShell as a DIY health monitoring solution.
My initial thought was to build a script that polls Hyper-V every few minutes to see if replication is working and to generate an alert if there is a problem. The more that I thought about it though, I began to realize that approach probably wasn’t the best option. My fear was that such a script would need to remain silent unless it detects a problem. Otherwise, clearing the output every few minutes would become obnoxious. The problem with running such a script invisibly however, is that the script could give me a false sense of security. If the script were to fail, then I might never know it, thereby putting my Hyper-V servers at risk of the same types of undetected failures that I was already working to resolve.
I decided that I needed to create a script that would occasionally confirm that it was working, wile staying silent most of the time. For example, such a script might email me a daily health report, but not send any additional emails throughout the day unless something is found to be wrong.
Unfortunately, writing a PowerShell script that can reliably send email messages is a very involved process. I might build email capabilities into the script at some point, but for the time being I needed to get monitoring capabilities up and running quickly.
Instead of using email, I decided to create pop-up messages. As my script is currently designed, a popup is generated first thing in the morning. After that, the script continues to run on a scheduled basis, but remains silent unless it finds a problem.
I have to admit that this approach to verifying my Hyper-V environment’s health seemed straightforward, but ultimately proved to be anything but. While I was able to come up with a workable solution, that solution pushed my PowerShell skills to the limit.
Much of the complexity stemmed from the fact that my Hyper-V hosts are domain joined, but my primary workstation (the Windows 11 computer where the script runs) is not. The trick was to enable PSRemoting on the Hyper-V machines and on my workstation. I also had to add my Hyper-V machines to my local computer’s list of trusted hosts. The command that I used was:
Set-Item WSMan:\localhost\Client\TrustedHosts -Value "Prod1,Prod2" -Concatenate
In this command, Prod1 and Prod2 are the names of my Hyper-V hosts.
In preparation for running my script, I also had to create a credential file. In order to do so, I had to open an elevated PowerShell session and run these commands:
$Cred = Get-Credential
$Cred | Export-Clixml C:\Healthcheck\hvcred.xml
These commands create a credential file called C:\HealthCheck\hvcred.xml. This file supplies the script with the credentials that it requires at run time. Although you must use an elevated PowerShell session to create the credential file, the main script does not require elevated credentials.
So with that said, my plan for this series is to show you the script and walk you through the basic setup process in Part 2. Then, in Part 3, I will show you how to run the script on a scheduled basis in a way that achieves the desired result.
About the Author
Brien Posey is a 22-time Microsoft MVP with decades of IT experience. As a freelance writer, Posey has written thousands of articles and contributed to several dozen books on a wide variety of IT topics. Prior to going freelance, Posey was a CIO for a national chain of hospitals and health care facilities. He has also served as a network administrator for some of the country's largest insurance companies and for the Department of Defense at Fort Knox. In addition to his continued work in IT, Posey has spent the last several years actively training as a commercial scientist-astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space. You can follow his spaceflight training on his Web site.