html 报告中的 Powershell 输出颜色

Powershell output color in html report

我一直在搜索和测试,但到目前为止没有任何乐趣。在以下脚本中,$statFix2 是一个重要的数字。我试图弄清楚如何在 html 报告中将其字体更改为红色,如果它的值高于“10”。我已经尝试过 IF 语句,但不确定将它放在哪里或正确的方法。有人有想法吗?

谢谢!

$smtp = "5.5.5.5"
$to = "x@x.com"
$from = "y@y.com"
$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

继续我的评论...

'powershell send html color'

命中 (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 luke@test.com -From ServerReport@test.com -Body $report -subject "Server Disk Space Report" -SmtpServer mysmtpserver.com

有模块可以使用...

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.                                                                                       
...
#>

不得不重做脚本,但我找到的唯一可行的解​​决方案来自这个线程: