June 3rd, 2011

Useless Typing…or Playing with PowerShell

Summary: Microsoft Windows PowerShell MVP, Sean Kearney, talks about a fun Windows PowerShell script he calls “useless typing.” Microsoft Scripting Guy, Ed Wilson, here. This week we will have one guest blogger for the entire week. Sean Kearney has written a series of blog posts about Windows PowerShell and the Legacy, as well as today’s blog about having some fun with Windows PowerShell. I am not going to be redundant by reposting his biography each and every day. Take it away, Sean… Sometimes we code in Windows PowerShell to improve our jobs at work. Then there are other times…well, we just play. Today I played. Maybe I over played… Several times, I’ve seen Start-Demo in action, which is a script to display a Windows PowerShell script while executing it. Presenters use it commonly. But I kept seeing it on the screen and thinking to myself, “I should make that look like somebody REALLY is typing it…” Well, that wasn’t difficult. A simple string with a bit of Random, like so:

$Sentence=“It was the best of times, it was the worst of times, because my Cat ate my lunch!”

foreach ($x in 1..$Sentence.Length) {

Write-host $Sentence[$x-1] –nonewline

Start-sleep –milliseconds (GET-RANDOM 100)

}

WRITE-HOST

   Great! I’m done! WRONG! You see, just like anything fun, once you start playing, things can get out of control.   I decided that I wanted NOISE! Some bizarre piece of my head snapped open and said, “There MUST be, nay… there SHALL be, the sound of a typewriter.” (Note: the complete Start-Typewriter Windows PowerShell script is located in the Scripting Guys Script Repository.) So I started digging for some typewriter sounds. I tried cheating by just using Invoke-Item, but the problem was that the sounds fell all over each other. So I dug online and found a great article written by Windows PowerShell MVP, Shay Levy, about Playing Sounds in PowerShell. It worked perfectly, except I needed a synchronous play (I wanted to make sure that nothing happened until the noise of the typing went away). So I assigned New-Object to $Sound per Shay’s article, and then I ran Get-Member against it, as shown here.

$SOUND = NEW-Object System.Media.SoundPlayer

$SOUND | GET-MEMBER

   I found a method called Playsync(), which met my needs perfectly. I turned that into a useful function in my script called PlaySound. If I pass it the location of the WAV file to play, it will play it.

function playsound {
        param($Location)
   
        $sound=new-object system.media.soundplayer
        $sound.SoundLocation=($Location)
        $sound.playsync()
    }

The next task I decided on was to have it pick that sound randomly. There are a bunch of ways to do it, but this is for fun. I stored them all in a variable called $List with Get-ChildItem pointing to the folder with the files.

$LIST=GET-CHILDITEM C:PointlessTypingCLACK?.WAV

At this point, I could just write a little function called PlayTypeSound that would (strangely enough) make a typing noise by playing a random one from the available WAV file list.

function playtypenoise {
        param($LIST)
        playsound (GET-RANDOM $LIST)
    }

At some point, my brain walked out the back door and decided that I should have the “ever perfect computer” make mistakes. So, as the song would go: “Conjunction junction, another function…” Every so often, I would have it type two completely random letters, mutter something, and then undo it, as shown here.

function oops {
       
            playtypenoise $LIST
   
            $typo1=[char](97+(GET-RANDOM 26))
            $typo2=[char](97+(GET-RANDOM 26))

            write-host $typo1$typo2 –nonewline

            playsound ‘C:Pointlesstypingdoh.wav’

            start-sleep -milliseconds (GET-RANDOM 100)

            $undo=”`b`b  `b`b”
           
            write-host $undo –nonewline

            $playtypenoise
            start-sleep -milliseconds (GET-RANDOM 100)
       
    }

   I even wrote a silly function called FlipCoin to decide (based on parameters) if the odds were against our typist friend.

function flipcoin {
    param($odds=25,$base=50)

    IF ((GET-RANDOM $BASE) -gt $odds) { return $TRUE }
}

   Then I went completely mad. I decided that a single line wasn’t enough. OH no…NOOOO! (Muah haha hahah!) I had to have a paragraph! So I tweaked the function a bit and pulled it together. Step through each line in the paragraph, type them suckers, make noise, irritate the neighbors…

foreach ($line in $paragraph) {


    $length=($line.Length)

    foreach ($x in 0..$length) {
           
        $letter=$line[$x]
       
        write-host $letter -NoNewline
   
        if (flipcoin 19) { playtypenoise $LIST }
 

        if (flipcoin 47) { oops }
}

So I played. I REALLY played. If you’re curious to hear and see this monstrosity, you can download my tiny ZIP file, and store the contents of all of this into a little folder called C:PointlessTyping. Run the script Start-Typing.PS1 to get it all into memory. Then execute either of the following:

START-TYPEWRITER –paragraph “To be or not to be, that is…. that is…. oh darn it what was my line?”

or

START-TYPEWRITER –paragraph (GET-CONTENT C:SomeReallyHonkingHugeTextFile.txt)

…and, errrr, be prepared to get some funny looks if you run it on a bus… Cheers! Sean, The Energized Tech Guest Blogger Week will continue tomorrow when Sean will talk about Windows PowerShell and working with legacy systems. This is a DO NOT DARE MISS series of vital blogs that has been months in the making. A special thank-you to Sean for writing this week’s blog posts. Hope you enjoy them. I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy

Author

0 comments

Discussion are closed.