Category Archive: Utility

IRC chat bot and monitor

Last weekend, I asked my wife if she could think of anything inappropriate or unusual to do with PowerShell. She came up with some good ideas and one of them was to write a chat/IM client.

A full client takes time, so with a nod toward PowerShell’s administrative uses, I wrote a script that forms the basis of an IRC chat bot.

This script can be used to pipe messages to a chat channel and/or join a set of channels, returning all messages posted there.

I’ve sure most of you out there use Microsoft MSN Windows Live Messenger, or even Yahoo! or AOL, so this may just serve as an example of synchronous TCP/IP messaging in PowerShell.

You can download the script here: chat-irc.ps1.

You may need to unblock it according the the instructions in help about_signing.

Read on »

Updated get-bufferhtml

A while back, I posted an article: Console screen grabs in html.

I fixed some bugs and added a couple of new features to the script get-bufferhtml.ps1, and have updated the article with examples.

Enjoy!

Banners

In the old days, when you printed something out, you’d have to wander down to the print room and find your output in a stack of fanfold with the job name printed as a large banner on the top. I miss those days so I wrote a banner script to do this:

banner screenshot

Read on »

Convert images to text (ASCII art)

Recently, whilst messing with sprites and the Grrr framework, I got thinking about better graphics for games.

As PowerShell is very slow at doing console atomic write operations, a game should really have fewer, larger sprites. I then thought about ASCII art, and wouldn’t it be great if PowerShell could convert existing images (jpg, png) and produce lines of text for use with Grrr’s create-image function.

So my morning’s work is a PowerShell script that does just that.

You can download it here: convert-image2text.ps1 (as a text file)

(If you’re reading this from the RSS feed, the link may be broken - open the blog page if so)

Unfortunately, it’s only monochrome. I had a lot of trouble getting a reasonable conversion of RGB or HSV to the console’s limited colour palette and character range, so in the end, I removed it.

The script uses System.Drawing and demonstrates one (a good?) way of adding an assembly to PowerShell.

Usage:

./convert-image2text.ps1 imagefile 
      [ -maxwidth nchars ] [-palette ascii | shade | bw ]

Here’s an example using the default -palette ascii option:

monolisa-ascii.gif

Here’s the same with the -palette shade option:

monolisa-shade.gif
Both are from this source image (click to see full size):


monalisa-small

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 »