ps1.soapyfrog.com

doing inappropriate things with powershell

Pages

  • About
  • Downloads
  • Archives

Search

Popular Posts

  • Grrr 1.1 and Big Invaders
  • Space Invaders
  • Convert images to text (ASCII art)
  • Cmdlet clashes
  • Console screen grabs in html

Recent Posts

  • Grrr source code, including Invaders
  • Google going down the pan
  • Cmdlet clashes
  • Grrr 1.1 and Big Invaders
  • Grrr, Cmdlets and PSInvaders revival

Categories

  • Announce (7)
  • Cmdlets (2)
  • Cool (16)
  • Grrr (6)
  • Hint (2)
  • Invaders (5)
  • Odd (2)
  • PowerShell (27)
  • Quiz (3)
  • Rant (7)
  • Uncategorized (1)
  • Utility (5)

Months

  • August 2007 (1)
  • April 2007 (1)
  • March 2007 (1)
  • February 2007 (3)
  • January 2007 (25)
  • December 2006 (1)

Bookmarks

  • Blogroll
    • $script Fanatics
    • blog.soapyfrog.com
    • Brian Long
    • Lee Holmes
    • Nik Crabtree
    • PowerShell-Scripting (French)
    • Richy Rich
    • The PowerShell Guy
    • Windows PowerShell
  • Links
    • Carbon-neutral web hosting!

Meta

  • Log in
  • Posts RSS
  • Comments RSS
  • Valid XHTML
  • Valid CSS
« Sudoku
Convert images to text (ASCII art) »
 

Console screen grabs in html

06 Jan 2007 07:22 pm// Cool, PowerShell, Utility    

[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.

There are a few options you can play with, all of them described in the comments at the top of the script.

One option is to change the palette. If we continue with the command get-bufferhtml -palette print > out2.html, we get 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 <<<< 
PS> get-bufferhtml > out.html

This is easier on the printer!

Another palette is “standard” - the one used by default if you open a “Command Prompt” or indeed any other console application. The key difference here is that DarkMagenta and DarkYellow are. I’ve no idea why the PowerShell team decided to mess the colour palette.

Continuing with the command get-bufferhtml -palette standard > out3.html, we get 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 <<<< 
PS> get-bufferhtml > out.html
PS> get-bufferhtml -palette print > out2.html

The default font is whatever your browser wants to use for the PRE tag, but you can override this if you issue a command like get-bufferhtml -palette print -font "tahoma" > out4.html and get:

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 <<<< 
PS> get-bufferhtml > out.html
PS> get-bufferhtml -palette print > out2.html
PS> get-bufferhtml -palette standard > out3.html

Clearly, fixed width fonts are best, but you can do whatever you like. I find that “Courier New” is best as the lines naturally line up without inter-line spacing.

4 comments to “Console screen grabs in html”

  1. On 25 Jan 2007 at 4:01 pm, Lance Robinson said:   

    Thanks Adrian, very useful!

  2. On 26 Jan 2007 at 4:00 pm, Damian Powell said:   

    That’s genius.

  3. On 10 Apr 2007 at 3:01 am, William said:   

    Does not seem to work on my IE7 system. See everything on 1 line.

  4. On 10 Apr 2007 at 6:53 am, adrian said:   

    Well, I know IE7 is pretty poor when it comes to rendering, but it’s not that bad :-)

    Maybe there’s something wrong with your IE7 set up.

    Send me a transcript of what you’re doing and the output if the problem persists.

Copyright © 2006-2008 Adrian Milliner

Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 2.5 License.