Powershell report output to HTML Formatting Issue

Hi all

I have been trying to produce a nice Powershell script on Critical Replication status of our Hyper-V clusters and export to a HTML report that is emailed daily.

I have used various scripts but the HTML formatting is usually terrible although have had some success with the below script with the exception that the colours are not behaving as expected.

For example I have colour labels to state the if “Critical” the field should be “Red” and if “Normal” the field should be “Green”, although when ran the field is usually always Green whatever the health status is.

See below script and any pointers would be very much appreciated?

# Set the formatting for the Table
$a = “<style>”
$a = $a + “BODY{background-color:white;}”
$a = $a + “BODY{font-family: Calibri; font-size: 11pt;}”
$a = $a + “TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}”
$a = $a + “TH{border-width: 2px;padding: 0px;border-style: solid;border-color: black;}”
$a = $a + “TD{border-width: 2px;padding: 0px;border-style: solid;border-color: black;}”
$a = $a + “</style>”

# Set Cell Colour for the replication status – green, orange, red
$StatusColor = @{Critical = ‘ bgcolor=”RED”>Critical<‘;Normal = ‘ bgcolor=”Green”>Normal<‘;Warning = ‘ bgcolor=”Orange”>Warning<‘;}

# Cluster Name
$ClusterName = “Hyper-VClusterName”

# Run Command to get Hyper-V Cluster Replication Statistics
$FullCommand = Get-ClusterNode -Cluster $ClusterName | foreach-object {Measure-VMReplication -ComputerName $_ | Select PrimaryServerName,Name,ReplicationState,CurrentReplicaServerName,LastReplicationTime,MissedReplicationCount,health} | ConvertTo-HTML -AS Table -Fragment | Out-String

# Cell Colour – Find\Replace
$StatusColor.Keys | foreach { $FullCommand = $FullCommand -replace “>$_<“,($StatusColor.$_) }

# Save the HTML Web Page
ConvertTo-HTML -head $a -PostContent $FullCommand -PreContent ‘<h1>Hyper-V Replication Status</h1>’ | Out-File c:\scripts\replication-health.html

# Send Email with contents
$body = Get-Content c:\scripts\replication-health.html -Raw
$smtp = “<SMTP Server IP/Hostname>”
Send-MailMessage -From hyperv@something.com -To “ITUser1@something.com”,”ITUser2@something.com” -CC “ITUser3@something.com” -Subject “Hyper-V Replication Status” -Body $body -SmtpServer $smtp -BodyAsHtml

Also if anyone has a better script that could be shared then I would also be extremely grateful as would really like to wrap this task up ASAP.

Many thanks


Can you give us an example of the output file? It may help identify which parts of the script are breaking, and allow us to give you some pointers on the script.

Also, it might be worth noting that the HTML & CSS forum may not be the best one for this particular question, so if needed, we can relocate it elsewhere.

1 Like


I’m unable to attach the output as I’m a new user :0(

Sorry but have just included the last 3 field of the output as server names are visible, but as you can see Critical should be coloured in RED and Normal should be GREEN, not sure what part of the HTML code is wrong?

Please any help appreciated.


Not sure that this has anything to do with the problem, but I notice critical bg color is uppercase and the others are capitalized.


1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.