0% found this document useful (0 votes)
34 views

Creating HTML Reports With Style

This document discusses techniques for creating HTML reports from PowerShell, including using CSS for styling, embedding images, and conditional formatting. Some key points covered are converting data to HTML fragments, appending or prepending text, embedding style information directly in the header or linking to an external CSS file, and programmatically adding styles like alternating row colors or highlighting based on cell values.

Uploaded by

Juan Leon
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Creating HTML Reports With Style

This document discusses techniques for creating HTML reports from PowerShell, including using CSS for styling, embedding images, and conditional formatting. Some key points covered are converting data to HTML fragments, appending or prepending text, embedding style information directly in the header or linking to an external CSS file, and programmatically adding styles like alternating row colors or highlighting based on cell values.

Uploaded by

Juan Leon
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Creating HTML Reports

with Style
Jeff Hicks
[email protected]
@jeffhicks
PowerShell HTML Basics
You must read help and examples for ConvertTo-HTML

Style done with CSS

Only convert the data that you need

Use html fragments

Append or prepend text to incorporate other information


Using HTML fragments

• Parse HTML for customizations


• Simple string
replacements
-Fragment
• Regular Expressions
• DOM -As
• Table*
• List
Do it with style

Use CSS file Embed style in Options


• Test browser the header • Alternate row
compatibility • Read from CSS file shading
• Good for published • Use a here-string • Conditional
reports formatting
• Chrome display • Embedded images
problem? First line = • javascript
@charset "UTF-8";
Alternate Rows
• Often used with tables <style>
body { background-color:#FFFFFF;
• Define a row background color font-family:Tahoma;
font-size:12pt; }
td, th { border:1px solid black;
border-collapse:collapse; }
th { color:white;
background-color:black; }
table, tr, td, th { padding: 2px; margin: 0px }
tr:nth-child(odd) {background-color: lightgray}
table { margin-left:50px; }
</style>
Conditional Formatting

Define
Parse
conditional Convert to [xml]$html = $drives | ConvertTo-Html -fragment
HTML and
style as a HTML
insert class
class
Conditional Formatting
<Title>Disk Drive Report</Title>
<style>
body { background-color:#FFFFFF;
font-family:Tahoma;
font-size:12pt; }
td, th { border:1px solid black;
Define border-collapse:collapse;
Parse
conditional Convert to }
HTML and
style as a HTML th { color:white;
insert class background-color:black; }
class
table, tr, td, th { padding: 5px; margin: 0px }
table { margin-left:50px; }
.danger {background-color: red}
.warn {background-color: yellow}
</style>
Conditional Formatting

for ($i=1;$i -le $html.table.tr.count-1;$i++) {


$class = $html.CreateAttribute("class")
#check the value of the last column and assign a class
Define if (($html.table.tr[$i].td[-1] -as [int]) -le 36) {
Parse $class.value = "danger"
conditional Convert to
HTML and $html.table.tr[$i].Attributes.Append($class) | Out-Null
style as a HTML }
insert class
class elseif (($html.table.tr[$i].td[-1] -as [int]) -le 50) {
$class.value = "warn"
$html.table.tr[$i].Attributes.Append($class) | Out-Null
}
}
Embedding Images
Referencing images by URL handy for intranet reports

Embed images to make files portable

Great for sending via email

Convert to base 64

• $ImageBits = [Convert]::ToBase64String((Get-Content $imagefile -Encoding Byte))

Insert stream as image source

• $ImageHTML = "<img src=data:image/png;base64,$($ImageBits) alt='disk


utilization'/>"
HTML PowerShell
Report Techniques
Resources
• The Lonely Administrator (http://jdhitsolutions.com/blog)
• PowerShell Deep Dives (ed. Jeffery Hicks)
• Creating HTML Reports in PowerShell by Don Jones
• PowerShell in Depth by Don Jones, Richard Siddaway & Jeffery Hicks
• http://PowerShell.org
Questions & Answers
Thank You

http://jdhitsolutions.com/blog

[email protected]

@JeffHicks

http://gplus.to/jeffhicks

You might also like