Sunday, July 8, 2012

Easy PowerShell 1: Objects


Function new-structureddata ($param1 = $null)
{
                $this = new-object PSObject
                $this | add-member -memberType NoteProperty -name DiscreteDataElement -value $param1
                $this | add-member -memberType ScriptMethod -name DoSomething -value {
                                $localVar = 1;
                                Return $this.DescreteDataElement + localVar1;
                }
                Return $this
}

When working with a unique set of data points, it may be easiest to create an object prototype so you don’t have declare every variable every time you want work with a particular data structure.  As an example, if you wanted to write a script to analyze baseball, you would probably want to create an object prototype to reflect baseball players.  You would then add variables to the object for things like position, height, weight, left or right handedness, etc.   These store data, so they would be created as the NoteProperty memberType.  You might want to perform calculations or other actions specific to a player.  These would be created as the ScriptMethod memberType.

In the snippet above, I’ve created a function (in this case it’s the powershell equivalent of a constructor (ask me if you really want to know more about constructors).  When you then call that function and assign it’s output to a variable, you will have a new object.
                $edgar = new-structuredData (“smith”)

This would create a structured data object with DiscreteDataElement set to “smith”.
You would then be able to access that by running $edgar.DiscreteDataElement.  The return would be smith.
If instead you ran $edgar.DoSomething you’d get “smith1” as a result.

In the case of our baseball player you might have a NoteProperty member of atBats and another of hits.  You could easily type out code to get the player’s batting average, but wouldn’t it be much easier to write it once, within the baseballplayer object, and simply call a function for the player’s batting average.

Let’s go ahead and build the baseball player

Function new-baseballplayer($first,$last)
{
                $this = new-object PSObject
                $this | add-member -memberType noteProperty -name firstname -value $first
                $this | add-member -memberTYpe noteProperty -name lastname -value $last
                $this | add-member -memberType noteProperty -name atBats -value 0
                $this | add-member -memberType noteProperty -name hits -value 0
                $this | add-member -memberType scriptMethod -name battingAverage -value {
                                $batAvg = $hits / $atBats * 1000;
                                Return $batAvg;
                }
}

Now, when we want to work with players we can simply create players.

$edgar = new-baseballplayer  “Edgar”,“Martinez”

And as you’re watching the game, edgar comes up to bat, so you run $edgar.atBats += 1
Unfortunately he struck out, so you don’t do anything with his hits value.
But next time around he does get a hit, so you run

$edgar.atBats += 1
$edgar.hits += 1

Now when you run
$edgar.battingAverage you’ll get
500

Obviously this is simplistic, and storing all your baseball players in your powershell session probably isn’t the best way to track stuff, but it gives you an idea of what’s possible with objects in powershell.

No comments:

Post a Comment

I look forward to your feedback and questions. The rules for commenting are simple. No personal attacks against other commenters. No threats.