Powershell脚本转换为html并根据数字阈值更改字体/单元格颜色

Powershell脚本转换为html并根据数字阈值更改字体/单元格颜色,html,css,powershell,Html,Css,Powershell,我有一个简单的powershell脚本,可以对获取cpu、磁盘和mem计数器的服务器列表运行多个查询。当csv被填充并转换为html时,我正在导入csv。。在脚本中也使用css来给行上色,但是如果大于80或小于5,我想突出显示单元格或文本为红色。 下面是代码的CSS部分,并导入CSV $css = @" <style> h1, h5, th { text-align: center; font-family: Segoe UI; } table { margin: auto; fon

我有一个简单的powershell脚本,可以对获取cpu、磁盘和mem计数器的服务器列表运行多个查询。当csv被填充并转换为html时,我正在导入csv。。在脚本中也使用css来给行上色,但是如果大于80或小于5,我想突出显示单元格或文本为红色。 下面是代码的CSS部分,并导入CSV

$css = @"
<style>
h1, h5, th { text-align: center; font-family: Segoe UI; }
table { margin: auto; font-family: Segoe UI; box-shadow: 10px 10px 5px #888; border: thin ridge grey; }
th { background: #0046c3; color: #fff; max-width: 400px; padding: 5px 10px; }
td { font-size: 11px; padding: 5px 20px; color: #000; }
tr { background: #b8d1f3; }
tr:nth-child(even){ background: #dae5f4; }
</style>
"@

Import-CSV "health.csv" | ConvertTo-Html -Head $css -Body "<h1>Email Report</h1>`n<h5>Generated on $(Get-Date)</h5>" | Out-File "health.html"
在本例中,您可以使用专用的自定义函数来创建Html表,该表允许您向单元格添加额外的样式,而不是使用ConvertTo-Html cmdlet

试试这个:

function ConvertTo-HTMLTable ($obj) {
    # add type needed to replace HTML special characters into entities
    Add-Type -AssemblyName System.Web

    $sb = New-Object -TypeName System.Text.StringBuilder
    [void]$sb.AppendLine('<table>')
    if ($null -ne $obj) {
        $headers = $obj[0].PSObject.Properties | Select -ExpandProperty Name
        [void]$sb.AppendLine('<thead><tr>')
        foreach ($column in $headers) {
            [void]$sb.Append(('<th>{0}</th>' -f [System.Web.HttpUtility]::HtmlEncode($column)))
        }
        [void]$sb.AppendLine('</tr></thead><tbody>')
        [double]$num = 0
        $obj | ForEach-Object {
            foreach ($column in $headers) {
                [string]$val = $_.$column
                # test if $val contains a number, and if so check if it is less than 5 or greater than 80
                if ([double]::TryParse($val, [System.Globalization.NumberStyles]::Float, 
                                       [System.Globalization.CultureInfo]::InvariantCulture, [ref]$num)) {
                    # it's a numeric value, see it we need to change color
                    $td = if ($num -gt 80 -or $num -lt 5) {"<td style=color:red;>$val</td>"} else {"<td>$val</td>"}
                }
                elseif ([string]::IsNullOrWhiteSpace($val)) { 
                    $td = '<td>&nbsp;</td>' 
                } 
                else { 
                    $td = '<td>{0}</td>' -f [System.Web.HttpUtility]::HtmlEncode($val)
                }
                [void]$sb.Append($td)
            }
            [void]$sb.AppendLine('</tr>')
        }
        [void]$sb.AppendLine('</tbody>')
    }
    [void]$sb.AppendLine('</table>')

    return $sb.ToString()
}



$css = @"
<style>
h1, h5, th, td { text-align: center; font-family: Segoe UI; }
table { margin: auto; font-family: Segoe UI; box-shadow: 10px 10px 5px #888; border: thin ridge grey; }
th { background: #0046c3; color: #fff; max-width: 400px; padding: 5px 10px; }
td { font-size: 11px; padding: 5px 20px; color: #000; }
tr { background: #b8d1f3; }
tr:nth-child(even){ background: #dae5f4; }
</style>
"@

$body  = "<h1>Email Report</h1>`r`n<h5>Generated on $(Get-Date)</h5>"
$table = ConvertTo-HTMLTable (Import-CSV "D:\health.csv")
$html  = @"
<!DOCTYPE html>
<html>
<head>
<title>Report</title>
<meta name="generator" content="PowerShell" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
$css
</head>
<body>
$body
$table
</body></html>
"@

$html | Out-File "D:\health.html" -Force
结果:


你能举一个.csv文件中行的例子吗?用csv样本更新帖子在你的PowerShell逻辑中,当你构建网页文档时,你可以使用基于标记的CSS格式吗?我们没有收到你的消息。。我的回答解决了你的问题吗?如果是,请点击✓ 在左边。这将帮助其他有类似问题的人更容易地找到它。
function ConvertTo-HTMLTable ($obj) {
    # add type needed to replace HTML special characters into entities
    Add-Type -AssemblyName System.Web

    $sb = New-Object -TypeName System.Text.StringBuilder
    [void]$sb.AppendLine('<table>')
    if ($null -ne $obj) {
        $headers = $obj[0].PSObject.Properties | Select -ExpandProperty Name
        [void]$sb.AppendLine('<thead><tr>')
        foreach ($column in $headers) {
            [void]$sb.Append(('<th>{0}</th>' -f [System.Web.HttpUtility]::HtmlEncode($column)))
        }
        [void]$sb.AppendLine('</tr></thead><tbody>')
        [double]$num = 0
        $obj | ForEach-Object {
            foreach ($column in $headers) {
                [string]$val = $_.$column
                # test if $val contains a number, and if so check if it is less than 5 or greater than 80
                if ([double]::TryParse($val, [System.Globalization.NumberStyles]::Float, 
                                       [System.Globalization.CultureInfo]::InvariantCulture, [ref]$num)) {
                    # it's a numeric value, see it we need to change color
                    $td = if ($num -gt 80 -or $num -lt 5) {"<td style=color:red;>$val</td>"} else {"<td>$val</td>"}
                }
                elseif ([string]::IsNullOrWhiteSpace($val)) { 
                    $td = '<td>&nbsp;</td>' 
                } 
                else { 
                    $td = '<td>{0}</td>' -f [System.Web.HttpUtility]::HtmlEncode($val)
                }
                [void]$sb.Append($td)
            }
            [void]$sb.AppendLine('</tr>')
        }
        [void]$sb.AppendLine('</tbody>')
    }
    [void]$sb.AppendLine('</table>')

    return $sb.ToString()
}



$css = @"
<style>
h1, h5, th, td { text-align: center; font-family: Segoe UI; }
table { margin: auto; font-family: Segoe UI; box-shadow: 10px 10px 5px #888; border: thin ridge grey; }
th { background: #0046c3; color: #fff; max-width: 400px; padding: 5px 10px; }
td { font-size: 11px; padding: 5px 20px; color: #000; }
tr { background: #b8d1f3; }
tr:nth-child(even){ background: #dae5f4; }
</style>
"@

$body  = "<h1>Email Report</h1>`r`n<h5>Generated on $(Get-Date)</h5>"
$table = ConvertTo-HTMLTable (Import-CSV "D:\health.csv")
$html  = @"
<!DOCTYPE html>
<html>
<head>
<title>Report</title>
<meta name="generator" content="PowerShell" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
$css
</head>
<body>
$body
$table
</body></html>
"@

$html | Out-File "D:\health.html" -Force