Archive for Saturday, January 6th, 2007

Console screen grabs in html

[Updated 25 Jan 2007, well rewritten :-) ]

Something I find myself doing a lot is getting a screen shot of a PowerShell console window and posting it some place. Unfortunately, its always a bitmap, and occasionally, I want it as formatted text.

Now, the console is readable via $host.ui.rawui.GetBufferContents, so I wrote a small(ish) script to do just that, outputting lines of html.

You can download the script here get-bufferhtml.ps1.

Put the script somewhere so you can run it by name.

By default, the script will grab lines from the top of the buffer up to, but not including the current line, so if you type in this sequence:

PS> get-bufferhtml > out.html
PS> get-date
PS> asdasda
PS> get-bufferhtml > out.html

You end up with this:

PS> get-bufferhtml > out.html
WARNING: There must be one or more lines to get
PS> get-date                                                            

25 January 2007 14:17:42                                                

PS> asdasda
The term 'asdasda' is not recognized as a cmdlet, function, operable pr
ogram, or script file. Verify the term and try again.
At line:1 char:7
+ asdasda < < < <  

Note that the first line produced a warning, the second showed the date, the third produced an error and the last command (not shown) produce the above.

That output was then just pasted into this blog entry.

Read on »