A Method to the Add-Member Madness

I have been writing a script where, for all intents and purposes, I basically needed to be able to create a class. I wanted to be able to repeatedly create a new-object of a certain type that had a certain set of properties and methods. Even in PowerShell V2, there is still no "New-Class" cmdlet, but we can fake it well enough for practical purposes. That is at least for my practical purposes. There is a lot of information on the web about using Add-Member and essentially creating property bags as shown in the code below.

001
002
003
004
005
006
007
008
009
$obj = new-object psobject 
                                                                     
$obj | Add-Member -MemberType noteproperty `
                  -Name FirstColor `
                  -Value orange                            

$obj | Add-Member -MemberType noteproperty `
                  -Name SecondColor `
                  -Value Yellow

From here we get a PSObject with two properties, FirstColor and SecondColor. So what can we do for methods? The Add-Member cmdlet has a parameter called MemberType. In the example above, I used two NoteProperties. There are several other possibilities that can be used here as well. I would argue, the second most useful MemberType would be ScriptMethod. ScriptMethods have a name, just like a NoteProperty, and a value, but the value is of type [ScriptBlock].

One thing to be aware of when using ScriptMethods is the automatic variable $this. The $this variable refers to the current object. It is similar to the "this" keyword in C#. Probably the best way to explain is with an example. First I will build a ScriptBlock using a here string and call the [ScriptBlock]::Create() Method. Finally, the ScriptBlock can be added as a value for the new member that is called "ChangeFirstColor"

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
$code = @'
param($color)
$this.FirstColor = $color
'@


$sblock = [scriptblock]::create($code)

$obj = new-object psobject 
                                                                     
$obj | Add-Member -MemberType noteproperty `
                  -Name FirstColor `
                  -Value orange                            

$obj | Add-Member -MemberType noteproperty `
                  -Name SecondColor `
                  -Value Yellow
                 
$obj | Add-Member -MemberType scriptmethod `
                  -Name ChangeFirstColor `
                  -Value $sblock

So now we have a method called ChangeFirstColor. Yes, we could just set the property to a new color, but suppose you wanted to put some logic and verification steps into it first. This is merely an example of what you could do. The other thing that is pretty cool is that ScriptMethods can take parameters. Notice I use the param statement in the ScriptBlock. You could also use $args but that drives me nuts and leads to confusion for the person that has to read code 6 months from now.

image

Comments (1) -

Comments are closed