如何在PowerShell生成的HTML表格中高亮AutoRenewal为No的行?
修改方案
修改后的完整脚本
### Create HTML $style = "<style>BODY{font-family: Arial; font-size: 8pt;}" $style += "TABLE{border: 1px solid black; border-collapse: collapse;}" $style += "TH{border: 1px solid black; background: #dddddd; padding: 5px; }" $style += "TD{border: 1px solid black; padding: 5px; }" $style += ".red-row { background-color: #ffcccc; color: #dc143c; }" # 背景浅红+文字深红,可按需调整 $style += "</style>" # Client Secret Reminder Notification #If (($ReminderSecretCount -gt 0) -and ($NewSecretCount -eq 0)){ # uncommnet if using NewSecretCount If ($ReminderSecretCount -gt 0){ # remove if using NewSecretCount $ReportDate = get-date -format d $headerHtml = "<H3>Client Secret Expiration Notification Report - $ReportDate</H3>" $headerHtml += "<H5>30 Day Client Secret Expiration Notification Summary</H5>" # 为符合条件的行添加样式类标记 $processedData = $ReminderSecret | Select-Object AppName, SecretName, EndDate, SentTo, AutoRenewal, @{ Name = 'RowClass' Expression = { if ($_.AutoRenewal -eq 'No') { 'red-row' } else { '' } } } | Sort-Object EndDate # 生成基础HTML,包含临时的RowClass列 $tableHtml = $processedData | ConvertTo-Html -Head $style -Body $headerHtml # 移除RowClass的表头列 $tableHtml = $tableHtml -replace '<th>RowClass</th>', '' # 将RowClass值转移到<tr>标签的class属性,并移除对应的<td>列 $tableHtml = $tableHtml -replace '<tr><td>(.*?)</td><td>(.*?)</td><td>(.*?)</td><td>(.*?)</td><td>(.*?)</td><td>(.*?)</td></tr>', { $class = $args[0].Groups[6].Value $rowContent = "<td>$($args[0].Groups[1].Value)</td><td>$($args[0].Groups[2].Value)</td><td>$($args[0].Groups[3].Value)</td><td>$($args[0].Groups[4].Value)</td><td>$($args[0].Groups[5].Value)</td>" if ($class) { "<tr class=`"$class`">$rowContent</tr>" } else { "<tr>$rowContent</tr>" } } $AppReport = $tableHtml -join "`n" }
核心修改点说明
- 新增CSS样式:在原有样式中添加
.red-row类,定义高亮行的背景色和文字颜色,可根据需求调整颜色值(比如只改文字颜色就删除background-color属性)。 - 标记目标行:通过
Select-Object的计算属性,为AutoRenewal值为No的行添加RowClass属性,值为red-row。 - 调整HTML结构:生成基础HTML后,用正则表达式移除临时的
RowClass列,并将类名转移到对应的<tr>标签上,实现行高亮效果。
内容的提问来源于stack exchange,提问作者Chabango
相关产品推荐
相关产品推荐

