Convertto-Html:突出显示具有特殊值的单元格? [英] Convertto-Html: Highlight the cells with special values?

查看:81
本文介绍了Convertto-Html:突出显示具有特殊值的单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的语句会生成一个位 html 表.

ps |转换为html

如果值大于,我想将CPU(s)"的文本(或整行)设为红色(将 css 类添加到 <td>),说,100?html 代码将用作电子邮件正文,因此 javascript 不是一个选项.

解决方案

我曾经在深夜玩 Powershell 时制作的示例(注意:代码未优化但可以用作灵感,因为它完全符合您的要求想).此示例基于 Exchange CMDlet,我想在其中突出显示超过 XXX GB 的邮箱.

$MailboxStatisticsList = Get-Mailbox |获取邮箱统计信息$Report = foreach($MailboxStatisticsList 中的 $Mailbox){$Split = $Mailbox.TotalItemSize.Split(" ")## 正确的新方法:我们正则表达式字节并将其转换为 MB$MailboxItemSize = $($Split[2] -replace '[^0-9]')/1MB[PSCustomObject] @{邮箱 = $Mailbox.DisplayNameTotalItemSize = $MailboxItemSize -as [int]} ## PSCUSTOMOBJECT 结束} ## FOREACH 循环结束$CSS = @'<风格>身体 {字体大小:14px;字体系列:宋体;}h2{文本对齐:居中;}div.topbar {高度:50px;宽度:100%;背景颜色:深红色;}table.mailboxsizetable {背景颜色:浅蓝色;边框:3px纯黑色;边界半径:5px;框阴影:10px 10px 5px #888888;}TD{边框:2px纯黑色;边界半径:5px;}tr.tablerow {背景颜色:浅绿色;}第 {背景颜色:黄色;边框:3px纯黑色;边界半径:5px;字体大小:16px;}td.toobig {背景颜色:红色;颜色:绿色;}div.tablediv 表{保证金:自动;}TD:悬停{背景颜色:白色;颜色:金色;字体粗细:粗体;}</风格>'@$BaseHTML = @'<div class="topbar">

<h2>服务报告</h2>'@## 转换为 HTML 并将表格导出为变量$HTMLTable = $Report |Sort-Object TotalItemSize -Descending |ConvertTo-Html -Fragment## 将 HTML 表格导入为 XML[xml]$XML = $HTMLTable## 创建属性类....$TableClass = $XML.CreateAttribute("class")## ....并赋予它值test"$TableClass.Value = "mailboxsizetable"## 现在我们把它粘在一起并附加它$XML.table.Attributes.Append($TableClass)## 输出 $XML.OuterXML 返回带有类的 HTML## 现在我们更进一步:表格行的条件格式(在我的待办事项列表中的个人<td>)## Foreach TR :foreach ($XML.table.SelectNodes("tr") 中的 $TableRow){##每个TR成为tablerow"类的成员$TableRow.SetAttribute("class","tablerow")## 如果行有 TD 并且 TD[1] 的状态为 running...if (($TableRow.td) -and ([int]$TableRow.td[1] -gt 2000)){## 用notrunning"类标记TD(应该使它成为一个Id)$TableRow.SelectNodes("td")[1].SetAttribute("class","toobig")}}## 添加代码:将表格包含在 div 标签中$FinalHTMLTable = [string]::Format('<div class="tablediv">{0}</div>',$XML.OuterXml)ConvertTo-Html -Head $CSS -Body ($BaseHTML + $FinalHTMLTable) |外文件 C:\TempFolder\mailexport.html

The following statement will generate a bit html table.

ps | convertto-html

I want to make the text (or the whole line) of "CPU(s)" in color red (add css class to the <td>) if the value is greater than, saying, 100? The html code will be used as email body so javascript is not an option.

解决方案

Sample I once made at a late night while playing around with Powershell (note : code is not optimized but it can be used as inspiration since it does exactly what you want). This sample was based on the Exchange CMDlets where I wanted to highlight mailboxes which were over 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

这篇关于Convertto-Html:突出显示具有特殊值的单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆