Fun with PowerShell

I have been trying to come up with the best way to start this blog. I have debated writing a post about why I am doing this blog, what it will be about, Resistentialism, me, etc. In the end I decided to share a share a stupid little scripting project I have been playing around with to help set the serious tone I believe this blog deserves.

I’ve been getting back into scripting lately since changing jobs. I have not needed todo all that much scripting in the past few years because I was in more of leadership role. Now I get to write scripts again, I am remember how much fun it is.

Back in the day, I stumbled upon a little script/program called the “Illuminator” that Steve Jackson Games was using to generate random, er, I mean coded, message from the “illuminated masters” of the Illuminati. I build something like it use a data file back then using VB 6.0 that I gave to SJG. (It was more or less useless to them considering that they are a Mac/Linux shop or at least were.) And now I use it as a way to learn various parts of new scripting languages when I need to. (I learn best “hands on”, and having a nice repeatable and expandable project to use for that makes things easier.) The project itself general requires using arrays, random numbers, files (writing to and reading from), and user input. (This example does not read from any files, but I am working on an expanded version that will. I will share that when it is complete. I also plan to write a Python version and possibly a Lua version as well.)

In the past, I would just generate random silly messages, but with the viral nature of Cave Johnson’s “Rue the day” speech from Portal 2, I decided to use that is the base message framework. If you have not played Portal 2 or hear of Cave Johnson, watch is video, thing will make a little more sense afterward:

Okay, with all that being said here is my silly little PowerShell “Rue the day” speech/message generator: (It has been testing with PowerShell 2 on Windows 7 64-bit)

##############################################################
# Name ruetheday.ps1
# Author: Taylor Kent
# Version: 1.0
# Description:
# 
# This script creates a random version on Cave Johnson's Rue The Day speech from Portal 2
# and either outputs it to the screen or a specifed file
# 
# You can add more things to the arrays used to generate the new version of the speech
# by modifying the code below
#
#
# Usage:
#
# ruetheday.ps1  [-number ] [-outfile ]
# 
# Usage Notes:
#
# Both parameters are optional
#
# History:
#
# 1.0 - Intial Creation
#
##############################################################

##############################################################
# Functions
##############################################################
# A standard way of displaying error text
function DisplayError
{
	param ([string]$errmsg, [string]$usageinfo)

	if ($usageinfo -ne "")
	{
$msg =@"
********************
ERROR: $errmsg
$usageinfo
********************
"@
	}
	else
	{
$msg =@"
********************
ERROR: $errmsg
********************
"@
	}
	Write-Host $msg -ForegroundColor red
}
# Check to see if a value is numeric
function isNumeric ($x) {
    $x2 = 0
    $isNum = [System.Int32]::TryParse($x, [ref]$x2)
    return $isNum
}
##############################################################
# Main
##############################################################
# Create Usage Info Constant
$USAGEINFO = @"
 ruetheday.ps1  [-number ] [-outfile ]
"@
# Check for commandline parameters
$quit = $false
$number = 1
$outfile = $null
if ($args.length -gt 0)
{
	for ($i = 0; $i -lt $args.Length; $i++)
	{
		switch ($args[$i])
		{
			"-?"
			{
				$quit = $true
				Write-Host "How to use:"
				Write-Host
				Write-Host $USAGEINFO
				Write-Host
			}
			"-help"
			{
				$quit = $true
				Write-Host "How to use:"
				Write-Host
				Write-Host $USAGEINFO
				Write-Host
			}
			"-number"
			{
				$i++
				if ((isNumeric $args[$i]) -eq $false)
				{
					$errmsg = $args[$i] + " is not a number and will be ignored"
					DisplayError $errmsg, $usageinfo
				}
				else
				{
					$number = [int]$args[$i]
				}
			}
			"-outfile"
			{
				$i++
				if ((test-path $args[$i] -IsValid) -eq $false)
				{
					$errmsg = $args[$i] + " is not a vaild path and will be ignored"
					DisplayError $errmsg, $usageinfo				
				}
				else
				{
					$outfile = $args[$i]
				}
			}
			default
			{
				$errmsg = $args[$i] + " is not vaild and will be ignored"
				DisplayError $errmsg, $usageinfo
			}
		}
	}
}
if ($quit -eq $false)
{
	for ($j=1; $j -le $number; $j++)
	{
		# Lists
		$thing1list = "life","John","the government","the king","the church","a dark mage","the system","Russia","Cthulhu","that stupid dog","the Devil","Darth Vader"
		$thing2list = "lemons","ice","taxes","bad music","undercooked pizza","mexican candy","a broken CD","crappy software"
		$thing3list = "don't make lemonade","don't make ice cubes","don't file a tax return", "don't just listen to it", "don't send it back", "don't just eat it and puke", "don't just eat it", "don't just install it and live with it"
		$thing4list = "house","refigerator","capitol building","studio","restaurant","candy factory","warehouse","computer"
		$thing5list = "engineers", "friends", "rebels","mages","wombat","army of cockroaches","Bucky Johnson","squid","Mad Scientists"
		$namelist = "Cave Johnson","Billy","Sheela","Merlin","Robin Hood","that guy","Richard Nixion"
		$pronounlist = "man","guy","woman","wizard","outlaw","guy","former president"
		
		# Get a random number generator
		$rand = New-Object System.Random
		
		# Pick stuff
		$thing1 = $thing1list[([int](($thing1list.Length - 1) * $rand.NextDouble()))]
		
		# $thing2 and $thing3 are linked
		$thing2number = [int](($thing2list.Length - 1) * $rand.NextDouble())

		$thing2 = $thing2list[$thing2number]
		$thing3 = $thing3list[$thing2number]

		$thing4 = $thing4list[([int](($thing4list.Length - 1) * $rand.NextDouble()))]

		# $name and $pronoun are linked
		$namenumber = [int](($namelist.Length - 1) * $rand.NextDouble())

		$name = $namelist[$namenumber]
		$pronoun = $pronounlist[$namenumber]

		$thing5 = $thing5list[([int](($thing5list.Length - 1) * $rand.NextDouble()))]

		# Generate Speech
$message = @"
Alright, I've been thinking. When $thing1 gives you $thing2, $thing3. Make $thing1 take the $thing2 back! Get mad! I don't want your damn $thing2! What am I supposed to do with these?! Demand to see $thing1 manager! Make $thing1 rue the day it though it could give $name $thing2! Do you know who I am? I'm the $pronoun who's gonna burn your $thing4 down! With $thing2! I'm going to get my $thing5 to invent a combustible $thing2 that burns your house down!
"@
		
		# If a file was supplied write the speech to it
		if ($outfile -ne $null)
		{
			$message | Out-File $outfile -Append
			"" | Out-File $outfile -Append
		}
		# Write the speech to the host
		Write-Host
		Write-Host $message
	}
	Write-Host

	$rand = $null
}

Here is a samples of it’s output:

Alright, I’ve been thinking. When Russia gives you undercooked pizza, don’t send it back. Make Russia take the undercooked pizza back! Get mad! I don’t want your damn undercooked pizza! What am I supposed to do with these?! Demand to see Russia manager! Make Russia rue the day it though it could give Merlin undercooked pizza! Do you know who I am? I’m the wizard who’s gonna burn your capitol building down! With undercooked pizza! I’m going to get my mages to invent a combustible undercooked pizza that burns your house down!

Got have fun with it. I look forward to your comments.

3 thoughts on “Fun with PowerShell

  1. Pingback: Message Generator | Republic of Resistentialism

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s