簡體   English   中英

Powershell 腳本轉換為 html 並根據數量閾值更改字體/單元格顏色

[英]Powershell script convert to html and change font/cell color based on number threshold

我有一個簡單的 powershell 腳本,它針對獲取 cpu、磁盤和內存計數器的服務器列表運行多個查詢。 Im importing the csv when its populated and converting to html.. also using css in the script to color the rows but i want to highlight and cell or text in RED if its above 80 or below 5. Here is the CSS portion of the code並導入 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 時可以嗎?

這是 CSV 的示例

"Server","CPU","MemFreeGB","CFreeGB","FFreeGB"
server1, 14.72, 17.18, 52.79, 80.42
server2, 0.2, 28.95, 59.86, 77.15
server3, 30.23, 13.5, 54.47, 83.14

在這種情況下,您可以使用專用的自定義 function 創建 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

結果:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM