以下语句将生成一个html表格。
ps | convertto-html
我想将“CPU(s)”的文本(或整行)变成红色(如果值大于100,则将css类添加到<td>
),说,100? html代码将用作电子邮件正文,因此javascript不是一个选项。
答案 0 :(得分:1)
您可以使用带有MatchEvaluator的正则表达式替换来查找值高于100
的行,并将style="color: #FF0000;"
添加到该行以使文本变为红色。
如果您只想通过将颜色样式添加到CPU字段的<td>
- 标记中来将CPU值设置为红色,则可以修改regex + matchevaluator。
实施例
#Get HTML
$html = ps | select name, cpu, Handles, FileVersion | convertto-html
#Get headers
$headers = [regex]::Matches(($html | out-string), "<th>(.*?)</th>")
#Find index of CPU-header
$cpu = $headers | ForEach-Object -Begin { $i = 0 } -Process { if($_.Groups[1].Value -eq 'CPU') { $i } else { $i++ } }
#Regex Replace MatchEvaluator
$ME = {
param($match)
#If Group 2 (CPU) is greater than 100
if([double]::Parse($match.Groups[2].Value) -gt 100) {
#Add red text-style to row
'<tr style="color: #FF0000;">{0}' -f $match.Groups[1].Value
} else {
#Return org. value
$match.Value
}
}
#Regex replace all lines
$body = $html | Foreach-Object { [regex]::Replace($_, "^<tr>((?:<td>[^<]*?<\/td>){$cpu}<td>(\d.*?)<\/td><.*)", $ME) }
答案 1 :(得分:1)
我在深夜玩Powershell时做的样本(注意:代码没有经过优化,但它可以用作灵感,因为它完全符合您的要求)。此示例基于Exchange CMDlet,我想要突出显示超过XXX GB的邮箱。
$MailboxStatisticsList = Get-Mailbox | Get-MailboxStatistics
$Report = foreach ($Mailbox in $MailboxStatisticsList)
{
$Split = $Mailbox.TotalItemSize.Split(" ")
## New way to get it right : we Regex the bytes and convert it to MB
$MailboxItemSize = $($Split[2] -replace '[^0-9]') / 1MB
[PSCustomObject] @{
Mailbox = $Mailbox.DisplayName
TotalItemSize = $MailboxItemSize -as [int]
} ## END OF PSCUSTOMOBJECT
} ## END OF FOREACH LOOP
$CSS = @'
<style>
body {
font-size : 14px;
font-family : arial;
}
h2 {
text-align: center;
}
div.topbar {
height : 50px;
width : 100%;
background-color : darkred;
}
table.mailboxsizetable {
background-color : lightblue;
border : 3px solid black;
border-radius : 5px;
box-shadow: 10px 10px 5px #888888;
}
td {
border : 2px solid black;
border-radius : 5px;
}
tr.tablerow {
background-color : lightgreen;
}
th {
background-color : yellow;
border : 3px solid black;
border-radius : 5px;
font-size : 16px;
}
td.toobig {
background-color : red;
color : green;
}
div.tablediv table {
margin : auto;
}
td:hover {
background-color : white;
color : gold;
font-weight : bold;
}
</style>
'@
$BaseHTML = @'
<div class="topbar">
</div>
<h2>Service Report</h2>
'@
## Convert to HTML and export table to a variable
$HTMLTable = $Report | Sort-Object TotalItemSize -Descending | ConvertTo-Html -Fragment
## Import the HTML table as XML
[xml]$XML = $HTMLTable
## Create the attribute Class....
$TableClass = $XML.CreateAttribute("class")
## ....and give it the value "test"
$TableClass.Value = "mailboxsizetable"
## Now we stick it together and append it
$XML.table.Attributes.Append($TableClass)
## Outputting $XML.OuterXML returns the HTML with the class
## Now we take it 1 step further : conditional formatting for the table rows (on individual <td> is on my ToDo list)
## Foreach TR :
foreach ($TableRow in $XML.table.SelectNodes("tr"))
{
## each TR becomes a member of class "tablerow"
$TableRow.SetAttribute("class","tablerow")
## If row has TD and TD[1] has the state running...
if (($TableRow.td) -and ([int]$TableRow.td[1] -gt 2000))
{
## tag the TD with the class "notrunning" (should make this an Id)
$TableRow.SelectNodes("td")[1].SetAttribute("class","toobig")
}
}
## Added code : enclose the table in a div tag
$FinalHTMLTable = [string]::Format('<div class="tablediv">{0}</div>',$XML.OuterXml)
ConvertTo-Html -Head $CSS -Body ($BaseHTML + $FinalHTMLTable) | Out-File C:\TempFolder\mailexport.html