In the previous tip we started to turn PowerShell results into HTML reports. The report content is now almost done. You just may want to polish some of the column headers and rename them. This is where we left off:

#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

To rename column headers, use the same strategy we used to turn non-string content into string content: use calculated properties. So if you’d like to rename “TimeGenerated” into “Time”, this is what you need to do:

#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 ',' }
}

$timeGenerated = @{
    # specify NEW name for column (property)
    Name = 'Time'
    # use existing value
    Expression = { $_.TimeGenerated }
}

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!

Anonymous