Creating HTML Reports (Part 2 – Fixing Non-String Content)

by Aug 10, 2017

In the previous tip we started to turn PowerShell results into HTML reports. So far, the report is produced but still looks ugly. This is where we start:

#requires -Version 2.0

$Path = "$env:temp\eventreport.htm"
$startDate = (Get-Date).AddHours(-48)

Get-EventLog -LogName System -EntryType Error -After $startDate |
  Select-Object -Property EventId, Message, Source, InstanceId, TimeGenerated, ReplacementStrings, UserName |
  ConvertTo-Html |
  Set-Content -Path $Path

Invoke-Item -Path $Path

When you run this code, the report reveals that there are some properties that contain non-string content. Take a look at column “ReplacementStrings”: the report contains the data type (string[] aka string array) instead of the actual data.

To fix problems like this, use a calculated property, and turn the content into readable text:

#requires -Version 2.0

$Path = "$env:temp\eventreport.htm"
$startDate = (Get-Date).AddHours(-48)

# make sure the property gets piped to Out-String to turn its
# content into readable text that can be displayed in the report
$replacementStrings = @{
    Name = 'ReplacementStrings'
    Expression = { ($_.ReplacementStrings | Out-String).Trim() }
}

Get-EventLog -LogName System -EntryType Error -After $startDate |
  # select the properties to be included in your report
  Select-Object -Property EventId, Message, Source, InstanceId, TimeGenerated, $ReplacementStrings, UserName |
  ConvertTo-Html |
  Set-Content -Path $Path

Invoke-Item -Path $Path

As you’ll see, the property now displays its content OK.

It is up to you just how you turn the property content into readable text. Piping the property to Out-String leaves the work to PowerShell’s internal magic. If you want more control, and if a property contained an array, you could as well use the -join operator to concatenate the array elements. This way, you get to choose which delimiter is used to separate the array elements. This example uses a comma:

#requires -Version 2.0

$Path = "$env:temp\eventreport.htm"
$startDate = (Get-Date).AddHours(-48)

# make sure the property gets piped to Out-String to turn its
# content into readable text that can be displayed in the report
$replacementStrings = @{
    Name = 'ReplacementStrings'
    Expression = { $_.ReplacementStrings -join ',' }
}

Get-EventLog -LogName System -EntryType Error -After $startDate |
  # select the properties to be included in your report
  Select-Object -Property EventId, Message, Source, InstanceId, TimeGenerated, $ReplacementStrings, UserName |
  ConvertTo-Html |
  Set-Content -Path $Path

Invoke-Item -Path $Path

Twitter This Tip! ReTweet this Tip!