title: PowerShell: ConvertTo-Html not respecting string, instead counting characters
body: <p>Thanks for any input, I'm writing a report and trying to compile the results into HTML for readability.  I'm trying to re-write some components to result in higher readability.  The problem seems to be associated with the conversion of data into HTML (via ConvertTo-Html).</p>
  
  <p><strong>Question</strong><br>
  How can I properly convert the string variable into HTML to compile in my report?</p>
  
  <p><strong>Code</strong></p>
  
  <pre><code>    # Querying specified event log for errors within the last 30 days
      $time = [System.Management.ManagementDateTimeConverter]::ToDmtfDateTime((Get-Date).AddDays(-30))
      $securityeventviewer = Get-WmiObject win32_ntlogevent -ComputerName SERVERNAME -ErrorAction Stop |
      Where-Object {($_.logfile -eq 'Security' -and $_.type -eq 'error' -and $_.timegenerated -ge $time)} |
      Select-Object @{n='ErrorTime';e={$_.ConvertToDateTime($_.TimeGenerated)}}, Category, EventCode, EventIdentifier, Type, Message |
      Sort-Object EventIdentifier -unique &lt;#|
      ConvertTo-Html -Fragment -PreContent '&lt;h2&gt;Security Event Log&lt;/h2&gt;'#&gt;
  
  if ($securityeventviewer -eq $null){
      $securityeventviewer = 'NO SECURITY ERRORS'
  
      }
  
  else {
      $securityeventviewer = $securityEventViewer
      }
  
  $securityeventviewer | ConvertTo-Html -Fragment
  </code></pre>
  
  <p><strong>Result</strong><br>
  It appears to be the properties of the variable, not the actual string:</p>
  
  <pre><code>&lt;table&gt;
  &lt;colgroup&gt;&lt;col/&gt;&lt;/colgroup&gt;
  &lt;tr&gt;&lt;th&gt;*&lt;/th&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;0&lt;/td&gt;&lt;/tr&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p><strong>Expected Result</strong><br>
  NO SECURITY ERRORS</p>
  
  <p>I've seen some posts that seem similar, but nothing exactly matching this question. It appears to be with the fact I'm trying to convert a string to HTML, but I don't know what I should do as a workaround.
  <em>The $securityeventviewer variable does have a value of NO SECURITY ERRORS, so I know the conversion is where the issue is occurring</em></p>
  
  <p>Thanks in advance for your responses!</p>
  
  <p><strong>UPDATED WORKING CODE TO CHECK</strong><br></p>
  
  <pre><code>    # Querying specified event log for errors within the last 30 days
  $time = [System.Management.ManagementDateTimeConverter]::ToDmtfDateTime((Get-Date).AddDays(-30))
  $securityeventviewer = Get-WmiObject win32_ntlogevent -ComputerName AUGURSERVER2k16 -ErrorAction Stop |
      Where-Object {($_.logfile -eq 'Security' -and $_.type -eq 'error' -and $_.timegenerated -ge $time)} |
      Select-Object @{n='ErrorTime';e={$_.ConvertToDateTime($_.TimeGenerated)}}, Category, EventCode, EventIdentifier, Type, Message |
      Sort-Object EventIdentifier -unique
  
  if ($securityeventviewer -eq $null){
      #If SQL instance and server don't match produce this result
      $securityeventviewer = "NO SECURITY ERRORS" |
      ForEach-Object {Add-Member -InputObject $_ -Type NoteProperty -Name Security -Value $_; $_} |
      Select Security |
      ConvertTo-Html -Fragment -PreContent '&lt;h2&gt;Security Event Log&lt;/h2&gt;'
      }
  
  else {
  $securityeventviewer = Get-WmiObject win32_ntlogevent -ComputerName AUGURSERVER2k16 -ErrorAction Stop |
      Where-Object {($_.logfile -eq 'Security' -and $_.type -eq 'error' -and $_.timegenerated -ge $time)} |
      Select-Object @{n='ErrorTime';e={$_.ConvertToDateTime($_.TimeGenerated)}}, Category, EventCode, EventIdentifier, Type, Message |
      Sort-Object EventIdentifier -unique |
      ConvertTo-Html -Fragment -PreContent '&lt;h2&gt;Security Event Log&lt;/h2&gt;'
      }
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <pre><code># Querying specified event log for errors within the last 30 days
  $securityEventViewer = Get-EventLog -LogName Security -After ([datetime]::Today).AddDays(-30) -EntryType Error -ComputerName AUGURSERVER2k16 -ErrorAction SilentlyContinue |
  Select-Object TimeGenerated, CategoryNumber, EventID, Index, EntryType, Message |
  Sort-Object Index -unique
  
  $output = $(
  if($securityEventViewer -eq $null){ 
      [PSCustomObject]@{'security' = "NO SECURITY ERRORS"} 
  } else { 
      $securityEventViewer 
  }
  ) 
  write-host $output
  </code></pre>