Category Archive: Cool

Grrr 1.1 and Big Invaders

So I’ve been spending a bit of time getting to grips with Cmdlets and resurrecting interest in PSInvaders using Grrr and now have something to post.

Grrr 1.1 is now Cmdlet based and comes as a Snap-In. The most obvious benefit from this is performance, as I can now present a playable version of PowerShell Big Invaders.

BigInvaders 1.1 in play

BigInvaders is one of the demo PowerShell scripts that makes use of the Grrr.

Download this prerelease version 1.1 of the Grrr snap-in.

Update: this should now install on Vista - thanks to Chris Warwick for pointing out some issues

There’s no installer (yet) so to get going, follow this instructions:

Unzip the archive where you want to use it and CD to the top level director where the README file is.

Type: ./installgrrr.ps1 -r

The -r switch forces it to re-register the snap-in if a (possibly) older version exists. It then adds the snap-in to the current shell.

From here you can CD into the demos directory and run any of the scripts. Each one shows a feature of Grrr, but perhaps the most interesting is in the biginvaders directory.

To run BigInvaders, you need to have a very large console. To achieve this you probably need to set the font size of your console window to 6×8.

If you want sound, you need to install DirectX DirectSound. More on why later.

It should work without, silently, but this hasn’t had much testing as all my XP boxes have it installed :-) If there are exceptions, start it with the -nosound switch.

Assuming all is well, type ./biginvaders.ps1 and you should see this title screen:

BigInvaders 1.1 intro screen

Hit ESC to quit or Space to play. In play, Space fires a missile and arrow keys move left and right. Hit F to toggle FPS display in the top-righthand corner.

The target FPS is 33. I achieve this easily on my MacPro, and my wife’s Dell (a core 1 duo, 1.66ghz) also just manages.

There are still a few snags to iron out here and I want to add proper PowerShell help and an installer, but it is functional.

I’ve learned a lot about C#, PowerShell SDK and .NET these last weeks and will write about my findings over the next days. It’s not all been rosy.

I’ll also be writing about the features of Grrr, and where I want to go with it.

That’s it for now.

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!

Webserver and RSH in PowerShell

Ever wanted to control a PowerShell session from a web browser or curl/wget on another machine?

Yes? Well it surprisingly simple using PowerShell to script the System.Net.HttpListener. Here’s a working example with some genuine uses (well useful for me).

I’ll leave all the security and risk concerns as an exercise for the reader :-)

Read on »

Running pipelines in the background

In the unix world, we’re used to being able to run the different commands in a pipeline in different threads/processes. This is usually a lot more efficient when the producer/consumer rates vary, and makes use of multiple cores/cpus.

For example, cmd1 | cmd2 will run the two commands in separate processes.

PowerShell doesn’t do this. Everything runs in one thread, and pipelined objects are processed in batches, one bit at a time. You can control the batch size with the -outBuffer parameter, but things are still done sequentially, with one command in the pipe being busy whilst all others are sitting idle.

Even the venerable CMD.exe will put these two commands tree | more into separate processes.

This aside, what about simply running an arbitrary command (or scriptblock) in the background whilst doing something else?

Read on »

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 »

Pipe or no pipe?

Many built-in cmdlets take input from the pipe, but alternatively let you specify an input with the -InputObject parameter. An example is the Get-Member cmdlet.

I want to be able to do this with my own functions, but as far as I can tell, there is no built-in mechanism for this for using an specified parameter instead of the pipe.

[Updated]

Read on »

Getting normality back

The PowerShelling that I do mostly involves messing around the console window - dimensions, colours, title, etc.

I wanted an easy way to get normality back so I added this to my $PROFILE. Nothing fancy or clever, but here it is:

Read on »

Space Invaders with sound!

After yesterday’s attempts to make noises, I added basic sound support to psinvaders.

On my setup, it seems that I can only play one sound at a time, so a new sound stops a previous, and it crackles, but that could be because of poor sound virtualisation in Parallels Desktop (yes, I do all my work on a Mac).

Download it here: psinvaders r94

Unpack the zipfile, then from the directory where psinvaders.ps1 is, type ./psinvaders.ps1

Enjoy!

Making noises

There are twothree ways I know of to make noises in PowerShell.

1. Simple and most boring: use the internal speaker’s beep.

write-host "`a`a`a"

2. Slightly better, use the .NET console beep. This plays A above middle C for one second.

[console]::beep(440,1000)

Both of the above are synchronous, ie, the script blocks until the sound has finished. Richy Rich is doing some interesting stuff with runspaces to do this asynchronously.

3. Lastly, you can play wav files. Sadly, only .wav files. No midi, mp3, mpeg4.. at least I couldn’t find anything in .net 2.

$file='C:\\WINDOWS\\Media\\Windows XP Startup.wav'
$sp = new-object Media.SoundPlayer ($file)
$sp.Load();
$sp.Play();
1..5 | % {
  echo "la la"
  sleep 1
}

This plays the wav asynchronously, so I feel should be used in a future version of psinvaders to get those authentic sounds.

So anyone know of a way to play midi files asynchronously in PowerShell with only a few lines of code?