Outputting HTML Reports

Share |

Join PowerShell.com!
Subscribe to Feed

PowerShell eBook
Sign up for
Your PowerTip of the Day:

Award-winning PowerShellPlus


Admin Guide to PowerShell Remoting


Mastering PowerShell eBook

PowerShell can export results as HTML. Simply pipe the results to ConvertTo-HTML and save the result in a file. When you do that, it is wise to use Select-Object to first limit the object properties to only those you want to see in your report, otherwise your HTML table gets huge.

Get-Service | Select-Object Status, Name, DisplayName | ConvertTo-HTML | Out-File report.htm
.\report.htm

The resulting report is a bit ugly, though. With the help of the optional parameters, -title, -body and -head, you can add colors, fonts and other design information. The most important customizing parameter is -head, allowing you to define styles for the different HTML elements in your report.

$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>Reporting Service Status</H1>"
$title = "Example HTML Output"

Get-Service |
Select-Object Status, Name, DisplayName |
ConvertTo-HTML -head $head -body $header -title $title |
Out-File report.htm
.\report.htm

Posted Jan 05 2009, 08:00 AM by ps1

Comments

jkavanagh58 wrote re: Outputting HTML Reports
on 01-05-2009 11:35 AM

small typo in the $head variable... remove the @' and '@

Aleksandar wrote re: Outputting HTML Reports
on 01-05-2009 1:27 PM

@' and '@ are parts of PowerShell's Here-String string. A Here-String is a string which starts with a @" and ends with a "@ (or @' and '@ if you don't want to expand the variable). Here-String is great when you want to assign a multi-line string value to a variable. The problem with the code above is that @' needs to be on a line all by itself. And same goes to the end of the here-string, '@.

Try this:

$head = @'

'@

Aleksandar wrote re: Outputting HTML Reports
on 01-07-2009 9:47 AM

All HTML code that I've put between $head = @' and '@ has disappeared. :(

Anyway, you can also find nice explanation about Here-String by Joe Pruitt ( devcentral.f5.com/.../powershell-abcs---h-is-for-here-string.aspx ).

A working programmer's notes wrote Things to Explore
on 05-18-2009 6:59 PM

Things to Explore

Copyright 2012 PowerShell.com. All rights reserved.