Windows PowerShell has many ways to present and store information. You can display it to the screen, write it to a file, send it to a printer, create an CSV or XML file or even a pretty HTML report. The ConvertTo-HTML cmdlet underwent a significant facelift for v2.0 and is a popular cmdlet. But I don't think admins know how to fully take advantage of the cmdlet or have the right tools.
For many admins, they might use ConvertTo-HTML in an expression like this.
[cc lang="PowerShell"]
Get-WMIObject -class win32_logicaldisk -filter "drivetype=3" -computername "Serenity","Quark","jdhit-dc01" |
Select -Property SystemName,DeviceID,VolumeName,Size,FreeSpace |
ConvertTo-HTML -Title "Drive Report" | out-file c:\work\basicinfo.html
[/cc]
This expression uses Get-WMIObject to connect to 3 computers, return some information about fixed disks, and create an HTML report. Remember, ConvertTo-HTML only does the conversion. You still need to send it to Out-File to save the results. I'll end up with a simple table like this. Very basic. Plain. Boring.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
Fortunately ConvertTo-HTML offers a number of parameters for creating some very nice looking reports. The cmdlet essentially takes input and creates a simple table. But you can insert arbitrary HTML code before the table using -PreContent as well as after the table with -PostContent. Use -Title to define an title (what else?). Real style though is done with a CSS file. Specify the CSS file with the -CSSUri parameter. I've included a sample css file you can download and use to get started.
Here's a more complete version of my basic example. Notice how I take advantage of the ConvertTo-HTML parameters.
[cc lang="PowerShell"]
Get-WMIObject -class win32_logicaldisk -filter "drivetype=3" -computername "Serenity","Quark","jdhit-dc01" |
Select -Property SystemName,DeviceID,VolumeName,@{Name="SizeGB";Expression={ "{0:N4}" -f ($_.Size/1GB)}},`
@{Name="FreeGB";Expression={ "{0:N4}" -f ($_.FreeSpace/1GB)}} |
ConvertTo-HTML -Title "Drive Report" `
-PreContent "
Fixed Disk Report
" `-CssUri ".\sample.css" -PostContent " report run $(get-date) by $env:userdomain\$env:username"|
out-file c:\work\fancyinfo.html
[/cc]
The CSS file is expected to be found in the same directory as the HTML file. Curious as to what I ended up with? Click here. It has been my experience that using CSS files works better when viewing the file in Internet Explorer. I'm not an HTML developer by any means so it's possible Firefox doesn't like the CSS files I use, but I get better results when viewing the final HTML in IE.
Feel free to use my sample css file and tweak as necessary. If you have a CSS file that creates very nice PowerShell reports, I hope you'll consider sharing.
For Firefox try using a dufferent DOCTYPE (4.0?) and be sure to use CSS 2.0 versions of all styles. The biggest differences between FF and IE are in DIV and TABLE tag behaviors. Ther eare numeraous liitle CSS tricks that will force both browsers to behave.
We are told that IE9 fixes all of this. HTML 5 and CSS3 are both very powerful and will, hopefully, end all the hoopla. From what I have seem I think MS has gotten it this time.
Very good write up. I will keep it for reference on ConvertTo-HTMJ to hand out to others.
Thanks for that explanation. My experience with CSS file is tweaking something someone else already created. I usually tell students to work with their web developers to get style sheets with corporate-approved formatting.
That is good advice. We can also put CSS style sheets on a network drive and use them.
ALso note that we can point at one style sheet and then add changes in a second or in a local ‘style’ block that can selectively override the master corporate style sheet.
Learning a bit about ‘style’ is a good thing if you do many HTML reports. The plus is that with PowerShell, as always, there are many ways to accomlish a task but most have someti8ng to do with ‘Standards’. Css is a standard that will continue to show up everywhere. XAML will even become slave to CSS as will all Windows Forms. It’s worth learning.
It just hit me that this might be useful and fun to play with.
To perform advanced controlled edits just use XML.
The DOCTYPE of teh output html is the following:
!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”
Notice that it is “XHTML strict” which makes it pure XML and can be edited with teh XMLDOM.
[xml]$xml =get-process|convertto-html
Note: To get to the html you will need to select into the declared namespace.