Creating HTML Reports (Part 3 – Adding Headers and Footers)

In the previous tip we started to turn PowerShell results into HTML reports. It now needs some headers and footers. This is where we left off:

#requires -Version 2.0

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

$replacementStrings = @{
    Name = 'ReplacementStrings'
    Expression = { $_.ReplacementStrings -join ',' }
}

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

To add content prior and/or after the data, use the -PreContent and -PostContent parameters. So to add a machine name as header, and a copyright notice as footer, try this:

#requires -Version 2.0

$Path = "$env:temp\eventreport.htm"
$today = Get-Date
$startDate = $today.AddHours(-48)
$startText = $startDate.ToString('MMMM dd yyyy, HH:ss')
$endText = $today.ToString('MMMM dd yyyy, HH:ss')

$preContent = "<h1>$env:computername</h1>
<h3>Error Events from $startText until $endText</h3>
"
$postContent = "<p><i>(C) 2017 SysAdmin $today</i></p>"

$replacementStrings = @{
    Name = 'ReplacementStrings'
    Expression = { $_.ReplacementStrings -join ',' }
}

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

Invoke-Item -Path $Path

Twitter This Tip! ReTweet this Tip!