My Daily Hyper-V Status Email–Part 2 of 5
Yesterday, I introduced my daily status email. Today I am going to talk about the first chunk of information that is included in this email: event logs.
The primary goal of my status email is to let me know if anything has gone wrong. By looking at any errors or warnings in the System and Hyper-V event logs; I can get a pretty good feel for the state of my servers. Here is a quick screenshot from a recent status email:
This display was generated with this code:
# EventLog
$message = $message + "<style>TH{background-color:$($errorColor)}TR{background-color:$($tableColor)}</style>"
$message = $message + "<B>Parent EventLog</B> <br> <br>"
$message = $message + "Errors: <br>" + ((Get-EventLog system -after (get-date).AddHours(-24) -entryType Error) | `
Select-Object @{Expression={$_.InstanceID};Label="ID"}, `
@{Expression={$_.Source};Label="Source"}, `
@{Expression={$_.Message};Label="Message"} `
| ConvertTo-HTML -Fragment) `
+ " <br>"
$message = $message + "<style>TH{background-color:$($warningColor)}TR{background-color:$($tableColor)}</style>"
$message = $message + "Warnings: <br>" + ((Get-EventLog system -after (get-date).AddHours(-24) -entryType Warning) | `
Select-Object @{Expression={$_.InstanceID};Label="ID"}, `
@{Expression={$_.Source};Label="Source"}, `
@{Expression={$_.Message};Label="Message"} `
| ConvertTo-HTML -Fragment) `
+ " <br>"
# Hyper-V EventLog
$message = $message + "<style>TH{background-color:$($errorColor)}TR{background-color:$($tableColor)}</style>"
$message = $message + "<B>Hyper-V EventLog</B> <br> <br>"
$message = $message + "Errors: <br>" + ((Get-WinEvent -FilterHashTable @{LogName ="Microsoft-Windows-Hyper-V*"; StartTime = (Get-Date).AddDays(-1); Level = 2}) | `
Select-Object @{Expression={$_.InstanceID};Label="ID"}, `
@{Expression={$_.Source};Label="Source"}, `
@{Expression={$_.Message};Label="Message"} `
| ConvertTo-HTML -Fragment) `
+ " <br>"
$message = $message + "<style>TH{background-color:$($warningColor)}TR{background-color:$($tableColor)}</style>"
$message = $message + "Warnings: <br>" + ((Get-WinEvent -FilterHashTable @{LogName ="Microsoft-Windows-Hyper-V*"; StartTime = (Get-Date).AddDays(-1); Level = 3}) | `
Select-Object @{Expression={$_.InstanceID};Label="ID"}, `
@{Expression={$_.Source};Label="Source"}, `
@{Expression={$_.Message};Label="Message"} `
| ConvertTo-HTML -Fragment) `
+ " <br>"
Deep inside this code are two basic cmdlets. Get-EventLog is used for getting entries from the System event log:
Get-EventLog system -after (get-date).AddHours(-24) -entryType Error
While Get-WinEvent is used for getting entries from the Hyper-V event logs:
Get-WinEvent -FilterHashTable @{LogName ="Microsoft-Windows-Hyper-V*"; StartTime = (Get-Date).AddDays(-1); Level = 2}
The rest of the code around these cmdlets performs the following operations:
- I use raw HTML to set the color of the table headers. Errors are put in tables with a red header, warnings get a yellow header.
- I run the output of these commands through Select-Object with the use of the “Expression” option to set column labels appropriately.
- Finally, I use ConvertTo-HTML –Fragment to get a nice HTML table outputted.
Tomorrow I will move on to showing how I generate information about the virtual machine state and replication health.
Cheers,
Ben
Comments
Anonymous
March 18, 2014
Thanks Ben, Good to see this initiative. I was in search for a consolidated report two months back and at last ended up on writing a similar one for myself. gallery.technet.microsoft.com/HyperV-Dash-Board-VM-Disk-299bac7d Looking forward to see the full report. Cheers Shaba shabarinath@live.comAnonymous
March 19, 2014
Shaba - Neat! A couple of people have emailed / tweeted me with pointers to their solutions. I am planning to give a call out at the end of this series and will include a link to your there. Cheers, Ben