根据值格式化 HTML 输出的列
Formatting columns of HTML output depending on their value
我正在尝试将条件格式应用于简单 PowerShell 函数的输出:
function Get-RamInfo {
$os = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $ComputerName
$cs = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $ComputerName
$memcalc = ($cs.totalphysicalmemory / 1GB) - ($os.freephysicalmemory / 1MB)
$calc = ($os.freephysicalmemory / 1MB) / ($cs.totalphysicalmemory / 1GB) * 100 -as [int]
$props = [ordered]@{'Total RAM (GB)'="{0:N2}" -f ($cs.totalphysicalmemory / 1GB);
'Total RAM In Use (GB)'="{0:N2}" -f ([math]::Round($memcalc,2));
'Total RAM Available (GB)'="{0:N2}" -f ($os.freephysicalmemory / 1MB);
'Total Ram Available (%)'= $calc;
}
$obj = New-Object -TypeName PSObject -Property $props
Write-Output $obj
}
这里是数据的实际处理和应用的逻辑:
$frag3 = Get-RamInfo -ComputerName $computername |
ConvertTo-Html -As Table -Fragment -PreContent '<h2>Memory Info</h2>' |
foreach {
if ($_.'Total Ram Available (%)' -lt 20) {
$_ -replace "<tr>", '<tr class="warning">'
} elseif ($_.'Total Ram Available (%)' -lt 10) {
$_ -replace "<tr>", '<tr class="danger">'
} else{
$_
}
} | Out-String
我只希望 if 语句在应用逻辑时引用标记为 "Total RAM Available (%)" 的列。例如。如果标号为"Total RAM Available (%)"的列的值小于20,则执行格式化,如果小于10,则执行不同的格式化,其他只是正常输出字符串而不格式化。
这是我得到的 HTML 输出:
<h2>Memory Info</h2>
<table>
<colgroup><col/><col/><col/><col/></colgroup>
<tr class="warning"><th>Total RAM (GB)</th><th>Total RAM In Use (GB)</th><th>Total RAM Available (GB)</th><th>Total Ram Available (%)</th></tr>
<tr class="warning"><td>31.96</td><td>7.44</td><td>24.52</td><td>77</td></tr>
</table>
- 我认为我没有正确引用单个列,因为它将格式应用于每个 table 单元格。
- 看来我选择的应用逻辑的方法很复杂。
在您的 if
语句看到数据时,您不再拥有要检查的对象和属性。使用正则表达式和 switch
语句从 HTML 字符串中提取数值:
$re = '<tr>(<td>[0-9.]+?</td><td>[0-9.]+?</td><td>([0-9.]+?)</td>)'
... | ForEach-Object {
if ($_ -match $re) {
switch ([int]$matches[2]) {
{$_ -lt 20} { $_ -replace $re, '<tr class="warning">' }
{$_ -lt 10} { $_ -replace $re, '<tr class="critical">' }
default { $_ }
}
}
} | ...
问题是,一旦您将对象转换为 HTML,您将无法将 if/else statemnet 用于其属性。我已经稍微编辑了你的脚本。它现在应该可以工作了。
function Get-RamInfo {
$os = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $computername
$cs = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $computername
$memcalc = ($cs.totalphysicalmemory / 1GB) - ($os.freephysicalmemory / 1MB)
$calc = ($os.freephysicalmemory / 1MB) / ($cs.totalphysicalmemory / 1GB) * 100 -as [int]
$props = [ordered]@{'Total RAM (GB)'="{0:N2}" -f ($cs.totalphysicalmemory / 1GB);
'Total RAM In Use (GB)'="{0:N2}" -f ([math]::Round($memcalc,2));
'Total RAM Available (GB)'="{0:N2}" -f ($os.freephysicalmemory / 1MB);
'Total Ram Available (%)'= $calc;
}
$obj = New-Object -TypeName PSObject -Property $props
Write-Output $obj
}
$raminfo = Get-RamInfo -ComputerName $computername |
Select -Property *,@{
name='class';e={
if ($_.'Total Ram Available (%)' -lt 20){'<tr class="warning">'}
elseif ($_.'Total Ram Available (%)' -lt 10){'<tr class="danger">'}
else{'<tr>'}
}
}
$frag3 = $raminfo | Select 'Total RAM (GB)','Total RAM In Use (GB)','Total RAM Available (GB)','Total Ram Available (%)' |
ConvertTo-Html -As Table -Fragment -PreContent '<h2>Memory Info</h2>' |
foreach { $_ -replace '<tr>',$($raminfo.class)
} | Out-String
我正在尝试将条件格式应用于简单 PowerShell 函数的输出:
function Get-RamInfo {
$os = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $ComputerName
$cs = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $ComputerName
$memcalc = ($cs.totalphysicalmemory / 1GB) - ($os.freephysicalmemory / 1MB)
$calc = ($os.freephysicalmemory / 1MB) / ($cs.totalphysicalmemory / 1GB) * 100 -as [int]
$props = [ordered]@{'Total RAM (GB)'="{0:N2}" -f ($cs.totalphysicalmemory / 1GB);
'Total RAM In Use (GB)'="{0:N2}" -f ([math]::Round($memcalc,2));
'Total RAM Available (GB)'="{0:N2}" -f ($os.freephysicalmemory / 1MB);
'Total Ram Available (%)'= $calc;
}
$obj = New-Object -TypeName PSObject -Property $props
Write-Output $obj
}
这里是数据的实际处理和应用的逻辑:
$frag3 = Get-RamInfo -ComputerName $computername |
ConvertTo-Html -As Table -Fragment -PreContent '<h2>Memory Info</h2>' |
foreach {
if ($_.'Total Ram Available (%)' -lt 20) {
$_ -replace "<tr>", '<tr class="warning">'
} elseif ($_.'Total Ram Available (%)' -lt 10) {
$_ -replace "<tr>", '<tr class="danger">'
} else{
$_
}
} | Out-String
我只希望 if 语句在应用逻辑时引用标记为 "Total RAM Available (%)" 的列。例如。如果标号为"Total RAM Available (%)"的列的值小于20,则执行格式化,如果小于10,则执行不同的格式化,其他只是正常输出字符串而不格式化。
这是我得到的 HTML 输出:
<h2>Memory Info</h2>
<table>
<colgroup><col/><col/><col/><col/></colgroup>
<tr class="warning"><th>Total RAM (GB)</th><th>Total RAM In Use (GB)</th><th>Total RAM Available (GB)</th><th>Total Ram Available (%)</th></tr>
<tr class="warning"><td>31.96</td><td>7.44</td><td>24.52</td><td>77</td></tr>
</table>
- 我认为我没有正确引用单个列,因为它将格式应用于每个 table 单元格。
- 看来我选择的应用逻辑的方法很复杂。
在您的 if
语句看到数据时,您不再拥有要检查的对象和属性。使用正则表达式和 switch
语句从 HTML 字符串中提取数值:
$re = '<tr>(<td>[0-9.]+?</td><td>[0-9.]+?</td><td>([0-9.]+?)</td>)'
... | ForEach-Object {
if ($_ -match $re) {
switch ([int]$matches[2]) {
{$_ -lt 20} { $_ -replace $re, '<tr class="warning">' }
{$_ -lt 10} { $_ -replace $re, '<tr class="critical">' }
default { $_ }
}
}
} | ...
问题是,一旦您将对象转换为 HTML,您将无法将 if/else statemnet 用于其属性。我已经稍微编辑了你的脚本。它现在应该可以工作了。
function Get-RamInfo {
$os = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $computername
$cs = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $computername
$memcalc = ($cs.totalphysicalmemory / 1GB) - ($os.freephysicalmemory / 1MB)
$calc = ($os.freephysicalmemory / 1MB) / ($cs.totalphysicalmemory / 1GB) * 100 -as [int]
$props = [ordered]@{'Total RAM (GB)'="{0:N2}" -f ($cs.totalphysicalmemory / 1GB);
'Total RAM In Use (GB)'="{0:N2}" -f ([math]::Round($memcalc,2));
'Total RAM Available (GB)'="{0:N2}" -f ($os.freephysicalmemory / 1MB);
'Total Ram Available (%)'= $calc;
}
$obj = New-Object -TypeName PSObject -Property $props
Write-Output $obj
}
$raminfo = Get-RamInfo -ComputerName $computername |
Select -Property *,@{
name='class';e={
if ($_.'Total Ram Available (%)' -lt 20){'<tr class="warning">'}
elseif ($_.'Total Ram Available (%)' -lt 10){'<tr class="danger">'}
else{'<tr>'}
}
}
$frag3 = $raminfo | Select 'Total RAM (GB)','Total RAM In Use (GB)','Total RAM Available (GB)','Total Ram Available (%)' |
ConvertTo-Html -As Table -Fragment -PreContent '<h2>Memory Info</h2>' |
foreach { $_ -replace '<tr>',$($raminfo.class)
} | Out-String