title: ConvertTo-HTML <td> Class
body: <p>I have a group of simple PowerShell functions that retrieve system statistics, convert them to HTML fragments &amp; then join it all together into one big HTML file at the end.</p>
  
  <p>I'm having a problem with some specific functions, whereas others appear to be working fine although I'm using exactly the same principle.</p>
  
  <p>This retrieves the system information:</p>
  
  <pre class="lang-bsh prettyprint-override"><code>function Get-ApplicationLogs {
      $query = '&lt;QueryList&gt;
                &lt;Query Id="0" Path="Application"&gt;
                &lt;Select Path="Application"&gt;*[System[(Level=1  or Level=2 or Level=3) and TimeCreated[timediff(@SystemTime) &amp;lt;= 86400000]]]&lt;/Select&gt;
                &lt;/Query&gt;
                &lt;/QueryList&gt;'
  
      $logs = Get-WinEvent -ComputerName $ComputerName -ErrorAction SilentlyContinue -FilterXml $query | Sort-Object {$_.Id}
  
      foreach ($log in $logs) {
          $props = [ordered]@{'Id'=$log.Id;
                    'Error Type'=$log.LevelDisplayName;
                    'Provider Name'=$log.ProviderName;
                    'Timestamp'=$log.TimeCreated;
                    'Message'=$log.Message;
                   }
  
          $obj = New-Object -TypeName PSObject -Property $props
  
          Write-Output $obj   
      }
  }
  </code></pre>
  
  <p>This applies the logic to the function and applies the conditional formatting for the  class of the output HTML code:-</p>
  
  <pre class="lang-bsh prettyprint-override"><code>$applogs = Get-ApplicationLogs -computername $computername  | 
             Select -Property *,@{
                 name='class';e={
                     if ($_.'Error Type' -match 'Error' -or 'Critical') {
                         '&lt;td class="danger"&gt;'
                     } elseif ($_.'Error Type' -match 'Warning') {
                         '&lt;td class="warning"&gt;'
                     } else {
                         '&lt;td&gt;'
                     }
                 }
             }
  </code></pre>
  
  <p>This binds it all together and executes the td class tag replacement:</p>
  
  <pre class="lang-bsh prettyprint-override"><code>$frag8 = $applogs | Select 'ID','Error Type','Provider Name','Timestamp','Message' | 
           ConvertTo-Html -As Table -Fragment -PreContent '&lt;hr&gt;', '&lt;h3&gt;Application Logs&lt;/h3&gt;' |
           foreach { $_ -replace '&lt;td&gt;',$($applogs.class) } |
           Out-String
  </code></pre>
  
  <p>But for some reason I get a very skewed HTML output, it seems to generate more  tags than required for each value:-</p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;hr&gt;
  &lt;h3&gt;Application Logs&lt;/h3&gt;
  &lt;table&gt;
  &lt;colgroup&gt;&lt;col/&gt;&lt;col/&gt;&lt;col/&gt;&lt;col/&gt;&lt;col/&gt;&lt;/colgroup&gt;
  &lt;tr&gt;&lt;th&gt;Id&lt;/th&gt;&lt;th&gt;Error Type&lt;/th&gt;&lt;th&gt;Provider Name&lt;/th&gt;&lt;th&gt;Timestamp&lt;/th&gt;&lt;th&gt;Message&lt;/th&gt;&lt;/tr&gt;
  &lt;tr&gt;
  &lt;td class="danger"&gt; &lt;td class="danger"&gt; &lt;td class="danger"&gt; &lt;td class="danger"&gt; &lt;td class="danger"&gt; &lt;td class="danger"&gt; &lt;td class="danger"&gt; &lt;td class="danger"&gt;10021&lt;/td&gt;
  &lt;td class="danger"&gt; &lt;td class="danger"&gt; &lt;td class="danger"&gt; &lt;td class="danger"&gt; &lt;td class="danger"&gt; &lt;td class="danger"&gt; &lt;td class="danger"&gt; &lt;td class="danger"&gt;Warning&lt;/td&gt;
  &lt;td class="danger"&gt; &lt;td class="danger"&gt; &lt;td class="danger"&gt; &lt;td class="danger"&gt; &lt;td class="danger"&gt; &lt;td class="danger"&gt; &lt;td class="danger"&gt; &lt;td class="danger"&gt;Windows Server Update Services&lt;/td&gt;
  &lt;td class="danger"&gt; &lt;td class="danger"&gt; &lt;td class="danger"&gt; &lt;td class="danger"&gt; &lt;td class="danger"&gt; &lt;td class="danger"&gt; &lt;td class="danger"&gt; &lt;td class="danger"&gt;08/08/2016 04:37:42&lt;/td&gt;
  &lt;td class="danger"&gt; &lt;td class="danger"&gt; &lt;td class="danger"&gt; &lt;td class="danger"&gt; &lt;td class="danger"&gt; &lt;td class="danger"&gt; &lt;td class="danger"&gt; &lt;td class="danger"&gt;The catalog was last synchronized successfully 1 or more days ago.&lt;/td&gt;
  &lt;/tr&gt;
  </code></pre>