Update 7/20/2015: This script is now available on GitHub at http://github.com/BenjaminArmstrong/Hyper-V-PowerShell/tree/master/Daily-Status-Report.
I have spent a fair amount of time creating a reliable and resilient Hyper-V deployment in my house. For the most part this system runs smoothly, however, the resiliency of the design leads to an interesting problem.
My Hyper-V servers are housed in a small closet off the side of my garage. And if something goes wrong (hard disk failure, system failure, etc…) everything keeps on running happily. And that is the problem, it should not run happily, it should tell me that something is wrong. If something is wrong I needed to know straight away – so I can replace broken parts, etc… before a bigger problem develops.
To address this issue– I have created a PowerShell script that runs on each server and sends me a daily status email.
I have been refining this status email script over the last year or so – and am very happy with the results. Some of the goals I had with this script were:
- Should work with my ISPs SMTP server (so it works even if my internal infrastructure is in a bad state)
- Should tell me everything I need to know about my servers
- Should allow me to tell the status at a glance
At this point in time, I get a daily status email with all the information I need, handily color coded so I can see quickly if everything is good or not:
This week I am going to step through all the parts of this script (it is quite a large script!) and explain what they do, and why they are there.
Today I am going to cover some of the “trimming” that is necessary to make this script work. Below is a code snippet which represents to top and bottom sections of my script – with all the data logic pulled out of the middle.
This is basically the code that is necessary to send an email using PowerShell. Note, there are simpler ways to send an email using PowerShell – but I am doing it with the Net.Mail.SmtpClient and Net.Mail.MailMessage objects for two reasons:
- My ISP (like many ISPs) requires that I authenticate to their SMTP server in order to send email. This script supports SMTP authentication.
- I really wanted a color coded email – so that problems would be bright red and grab my attention. To do this properly means that I need to be able to generate a HTML based email, not just a plain text one.
The rough summary of the code above is:
- All the necessary variables are established (note – I have removed my email addresses, usernames and passwords – so you will need to swap in appropriate values here).
- A connection to the SMTP server is created
- A HTML header is created for the email
- There is a whole bunch of code removed (that I will be covering over the course of this week)
- The email message is constructed, using the HTML message string as the body
- Finally, the email is sent using the SMTP server
Over this week I will be covering the sections of this script that:
- Gather key event log information
- Gather VM and VM Replication health information
- Gather Storage health information
- Gather utilization data about the virtual machines
The final result – for those who are too impatient to wait out the week – is the script that I have attached to the end of this blog post. I have this script setup to run once a day using a scheduled task.