Console screen grabs in html

Home  >>  Cool  >>  Console screen grabs in html

Console screen grabs in html

On January 6, 2007, Posted by , In Cool,PowerShell,Utility, With 4 Comments

[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 so far:

  1. Thanks Adrian, very useful!

  2. Damian Powell says:

    That’s genius.

  3. William says:

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

  4. adrian says:

    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.