Creating HTML Reports

PowerShell can convert objects into HTML using ConvertTo-HTML. By adding a bit of custom formatting, your reports can be colorful and cool. The following example shows how to retrieve all error event log entries from all eventlogs and nicely output them as HTML report.

Note that the code will only include the local system. Need to include more than one system in your report? Simply add more IP-addresses or computer names as a comma-separated list::

$head = '<style>
BODY{font-family:Verdana; background-color:lightblue;}
TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
TH{font-size:1.3em; border-width: 1px;padding: 2px;border-style: solid;border-color: black;background-color:#FFCCCC}
TD{border-width: 1px;padding: 2px;border-style: solid;border-color: black;background-color:yellow}
</style>'

$header = "<H1>Last 24h Error Events</H1>"
$title = "Error Events Within 24 Hrs"

"127.0.0.1" | ForEach-Object {

$time = [System.Management.ManagementDateTimeConverter]::ToDmtfDateTime((Get-Date).AddHours(-24))
Get-WmiObject win32_NTLogEvent -computerName $_ -filter "EventType=1 and TimeGenerated>='$time'" |
ForEach-Object { $_ | Add-Member NoteProperty TimeStamp (
[System.Management.ManagementDateTimeConverter]::ToDateTime($_.TimeWritten)) ; $_ }
} |
Select-Object __SERVER, LogFile, Message, EventCode, TimeStamp |
ConvertTo-Html -head $head -body $header -title $title |
Out-File $home\report.htm

& "$home\report.htm"

Posted Mar 20 2009, 08:00 AM by ps1
Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.