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:

image

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