Powershell output color in html report

1.6k views Asked by At

I've been searching and testing but so far no joy. In the following script $statFix2 is an important number. I'm trying to figure out how in the html report to change its font to Red if its value is above '10'. I've tried IF statements but not sure where to put it or the correct method. Anyone have an idea?

Thanks!

$smtp = "5.5.5.5"
$to = "[email protected]"
$from = "[email protected]"
$subject = "Replication Status"
$header = @"
<style>
TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #B4DFFF;}
TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
</style>
"@

#create report array and foreach loop. This requires a text file with the different consistency group names(case sensitive) in it.
$report = @()
ForEach ($cg in (get-content C:\Scripts\HealthCheckScript\consistencyGroups.txt)){
     $tempCgState = (get-content C:\Scripts\HealthCheckScript\cgState.txt)
     $tempCgStats = (get-content C:\Scripts\HealthCheckScript\cgStats.txt)
     $statFix = $tempCgStats |select-string "WAN traffic"
     $statfix1 = $tempCgStats |Select-String "Current image"
     $statfix2 = $tempCgStats |Select-String "Journal lag"
     $statfix3 = $tempCgState |Select-String "Data Transfer"
     
     $row = "" | Select Consistency_Group, Sync_Status, Transfer_Rate, Journal_Current_Image, Journal_Lag_Status
          $row.Consistency_Group = $cg
          $row.Sync_Status = $statFix3 
          $row.Transfer_Rate = $statFix
          $row.Journal_Current_Image = $statFix1
          $row.Journal_Lag_Status = $statFix2
          $report += $row
}

#create Email body using the report and the html style defined above
$body = $report| ConvertTo-HTML -Head $header |out-string

#send the email
Send-MailMessage -SmtpServer $smtp -To $to -From $from -Subject $subject -Body $body -BodyAsHtml
2

There are 2 answers

0
Ken On BEST ANSWER

Had to redo the script, but the only solution I have found yet that works is from this thread:

Color-code cells in HTML mail based on criteria

1
postanote On

Continuing from my comment...

'powershell send html color'

hit(S)s...

Setting HTML font color in Powershell email

Sending HTML emails with PowerShell and zero HTML knowledge required

$disks = GET-WMIOBJECT win32_logicaldisk -filter "DriveType='3'"

foreach($disk in $disks)
{
$DriveLetter = $disk.DeviceID;
$SizeGB = $disk.Size / 1GB -as [int]
$FreeSpaceGB = $disk.FreeSpace / 1GB -as [int]
$PercentFree = [math]::Round((1- ($freeSpaceGB/ $sizeGB)) * 100)

$dataRow = "
</tr>
<td>$DriveLetter</td>
<td>$SizeGB GB</td>
<td>$FreeSpaceGB GB</td>
<td>$PercentFree %</td>
</tr>
"
$diskreport += $datarow

}

$report = "<html>
<style>
{font-family: Arial; font-size: 13pt;}
TABLE{border: 1px solid black; border-collapse: collapse; font-size:13pt;}
TH{border: 1px solid black; background: #dddddd; padding: 5px; color: #000000;}
TD{border: 1px solid black; padding: 5px; }
</style>
<h2>Server Space Report</h2>
<table>
<tr>
<th>Volume</th>
<th>Total Space</th>
<th>Free Space</th>
<th>Percent Full</th>
</tr>
$diskreport
</table>
<tr>
"

Send-MailMessage -To [email protected] -From [email protected] -Body $report -subject "Server Disk Space Report" -SmtpServer mysmtpserver.com

There are modules to use...

Find-Module -Name '*html*' | Format-Table -AutoSize

# Results
<#
Version  Name              Repository Description                                                                                                                  
-------  ----              ---------- -----------                                                                                                                  
0.0.95   PSWriteHTML       PSGallery  Module that allows creating HTML content/reports in a easy way.                                                              
...
0.1.7    PowerHTML         PSGallery  Provides a wrapper for HTML Agility Pack for use where the IE HTML DOM from Invoke-WebRequest is not available such as Pow...
1.4.1.2  ReportHTML        PSGallery  A powerful module for creating HTML reports within PowerShell no HTML coding required.  For more details on what is possib...
...
0.1.1    HtmlReport        PSGallery  Generate pretty HTML reports with tables and charts                                                                          
1.0.1    Write-HtmlNode    PSGallery  Writes the given HTML node with color.                                                                                       
...
#>