gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-27 23:16:43 26 4
gpt4 key购买 nike

我有一个简单的 powershell 脚本,它针对获取 cpu、磁盘和内存计数器的服务器列表运行多个查询。我在填充 csv 并将其转换为 html 时导入它。还在脚本中使用 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"

使用convert to 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

最佳答案

在这种情况下,您可以使用专用的自定义函数来创建 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

结果:

enter image description here

关于html - Powershell 脚本转换为 html 并根据数字阈值更改字体/单元格颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58240836/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com