My Daily Hyper-V Status Email–Part 3 of 5

Continuing on with my daily status email series; after displaying event log information, my email displays a high level summary of the virtual machine health:


These tables are generated with the following code:

# VM Health
$message = $message + "<style>TH{background-color:Indigo}TR{background-color:$($errorColor)}</style>"
$message = $message + "<B>Virtual Machine Health</B> <br> <br>"
$message = $message + "Virtual Machine Health: <br>" + ((Get-VM | `
                                                          Select-Object @{Expression={$_.Name};Label="Name"}, `
                                                                        @{Expression={$_.State};Label="State"}, `
                                                                        @{Expression={$_.Status};Label="Operational Status"}, `
                                                                        @{Expression={$_.UpTime};Label="Up Time"} `
                                                                        | ConvertTo-HTML -Fragment) `
                                                                        | %{if($_.Contains("<td>Operating normally</td>")){$_.Replace("<tr><td>", "<tr style=`"background-color:$($warningColor)`"><td>")}else{$_}} `
                                                                        | %{if($_.Contains("<td>Running</td><td>Operating normally</td>")){$_.Replace("<tr style=`"background-color:$($warningColor)`"><td>", "<tr style=`"background-color:$($tableColor)`"><td>")}else{$_}}) `
                                                                        + " <br>"
# VM Replication Health
$message = $message + "<style>TH{background-color:Indigo}TR{background-color:$($errorColor)}</style>"
$message = $message + "<B>Virtual Machine Replication Health</B> <br> <br>"
$message = $message + "Virtual Machine Replication Health: <br>" + ((Get-VM | `
                                                          Select-Object @{Expression={$_.Name};Label="Name"}, `
                                                                        @{Expression={$_.ReplicationState};Label="State"}, `
                                                                        @{Expression={$_.ReplicationHealth};Label="Health"}, `
                                                                        @{Expression={$_.ReplicationMode};Label="Mode"} `
                                                                        | ConvertTo-HTML -Fragment) `
                                                                        | %{if($_.Contains("<td>Replicating</td><td>Normal</td>")){$_.Replace("<tr><td>", "<tr style=`"background-color:$($tableColor)`"><td>")}else{$_}}) `
                                                                        + " <br>"

Both of these tables are generated by taking the output of “Get-VM” and displaying different information.

Notes about this code:

  • Once again – I use raw HTML to set the color of the table headers. 
  • Again – I run the output of these commands through Select-Object with the use of the “Expression” option to set column labels appropriately.
  • Again – I use ConvertTo-HTML –Fragment to get a nice HTML table outputted.
  • This time I do something different to get color coding for individual entries in the table.  I actually set each table cell to be “red” by default.  I then do some string parsing to see if the health is good – and switch the background color if I get a positive result.  The reason why I use this approach is that the list of “known good states” is much smaller than the list of “known bad states”.