【发布时间】:2016-07-22 10:29:30
【问题描述】:
我正在尝试将条件格式应用于一个简单的 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 语句在应用逻辑时引用标有“可用总 RAM (%)”的列。例如。如果标有“可用总 RAM (%)”的列的值小于 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>
- 我认为我没有正确引用单个列,因为它将格式应用于每个表格单元格。
- 看来我选择应用逻辑的方法相当复杂。
【问题讨论】:
标签: html powershell powershell-5.0