Powershell… Making it difficult for yourself!

Today I was reading the post Powershell is Really Easy… If you know what you’re doing and it really struck a chord with me.

A few days ago I discovered the ConvertTo-HTML cmdlet that can easily convert Powershell objects into formatted html pages. As I’ve written a few scripts producing html output, by sticking together a whole bunch of html tags, this was a real treat!

This simple one-liner will produce a html page from the output of the Get-Service cmdlet.

?View Code POWERSHELL
Get-Service | ConvertTo-Html | Out-File C:\Users\Rhys\Desktop\services.html;

ConvertTo-HTML Output from Get-Service

As usual Powershell gives us loads of options to use with this cmdlet. We can specify a css file uri, title tags and if objects should be formatted as a list or a table. I do a lot of formatting, that the cmdlet can’t accommodate, in my html reports so I like using the –Fragment parameter which will output the html for a table only. This allows me to produce some fairly fancy looking reports by adding custom html.

?View Code POWERSHELL
Get-Service | Select-Object -First 1 | ConvertTo-Html -Fragment;

This spits out html like below…

<table>
<colgroup>
<col/>
<col/>
<col/>
<col/>
<col/>
<col/>
<col/>
<col/>
<col/>
<col/>
<col/>
<col/>
<col/>
<col/>
<col/>
</colgroup>
<tr><th>Name</th><th>RequiredServices</th><th>CanPauseAndContinue</th><th>CanShutdown</th><th>CanStop</th><th>DisplayName</th><th>DependentServices</th><th>MachineName
</th><th>ServiceName</th><th>ServicesDependedOn</th><th>ServiceHandle</th><th>Status</th><th>ServiceType</th><th>Site</th><th>Container</th></tr>
<tr><td>ACDaemon</td><td>System.ServiceProcess.ServiceController[]</td><td>False</td><td>False</td><td>False</td><td>ArcSoft Connect Daemon</td><td>System.ServiceProce
ss.ServiceController[]</td><td>.</td><td>ACDaemon</td><td>System.ServiceProcess.ServiceController[]</td><td>SafeServiceHandle</td><td>Stopped</td><td>Win32OwnProcess</
td><td></td><td></td></tr>
</table>

So generally it’s much easier than cobbling together html tags yourself. I guess the lesson here is that I often tend to pick and run with something a bit prematurely. I should probably spend more time reading the documentation and learning a little lot more about what is available. Time for me to get digging into One Cmdlet at a time.


Leave a Reply