title: Formatting columns of HTML output depending on their value
body: <p>I'm trying to apply conditional formatting to the output of a simple PowerShell function:</p>
  
  <pre class="lang-bsh prettyprint-override"><code>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
  }
  </code></pre>
  
  <p>Here is the actual processing of the data and the logic applied:</p>
  
  <pre class="lang-bsh prettyprint-override"><code>$frag3 = Get-RamInfo -ComputerName $computername |
           ConvertTo-Html -As Table -Fragment -PreContent '&lt;h2&gt;Memory Info&lt;/h2&gt;' |
           foreach {
               if ($_.'Total Ram Available (%)' -lt 20) {
                   $_ -replace "&lt;tr&gt;", '&lt;tr class="warning"&gt;'
               } elseif ($_.'Total Ram Available (%)' -lt 10) {
                   $_ -replace "&lt;tr&gt;", '&lt;tr class="danger"&gt;'
               } else{
                   $_
               }
           } | Out-String
  </code></pre>
  
  <p>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.</p>
  
  <p>Here is the HTML output I get:</p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;h2&gt;Memory Info&lt;/h2&gt;
  &lt;table&gt;
  &lt;colgroup&gt;&lt;col/&gt;&lt;col/&gt;&lt;col/&gt;&lt;col/&gt;&lt;/colgroup&gt;
  &lt;tr class="warning"&gt;&lt;th&gt;Total RAM (GB)&lt;/th&gt;&lt;th&gt;Total RAM In Use (GB)&lt;/th&gt;&lt;th&gt;Total RAM Available (GB)&lt;/th&gt;&lt;th&gt;Total Ram Available (%)&lt;/th&gt;&lt;/tr&gt;
  &lt;tr class="warning"&gt;&lt;td&gt;31.96&lt;/td&gt;&lt;td&gt;7.44&lt;/td&gt;&lt;td&gt;24.52&lt;/td&gt;&lt;td&gt;77&lt;/td&gt;&lt;/tr&gt;
  &lt;/table&gt;
  </code></pre>
  
  <ol>
  <li>I don't think I'm referencing the single column correctly as it's applying the formatting to every table cell.</li>
  <li>It seems that the method I'm choosing to apply the logic is quite convoluted.</li>
  </ol>