I'm trying to apply conditional formatting to the output of a simple PowerShell function:
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
}
Here is the actual processing of the data and the logic applied:
$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
I only want the if statement to reference the column labelled "Total RAM Available (%)" when applying the logic. E.g. If the value of the column labelled "Total RAM Available (%)" is less than 20, then execute the formatting, if it's less than 10, then execute different formatting, for anything else just output the string as normal without formatting.
Here is the HTML output I get:
<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>
- I don't think I'm referencing the single column correctly as it's applying the formatting to every table cell.
- It seems that the method I'm choosing to apply the logic is quite convoluted.