The past few weeks I’ve been working on a Powershell OO scripting framework. I figure I better go ahead and publish it because I could keep tinkering on it for a long time. I suspect it’s not done and even has a bug or two but it’s in decent shape. While this is not “real” Object Oriented it does support:
· Polymorphism
· Encapsulation
· Constructors with parameters
· Notes – read-write variables
· Methods – scriptblocks
· Properties with Get scriptblocks and optional Set scriptblocks
· Static and Private Notes and Methods
The New-PSClass cmdlet takes a script which acts as Class Definition. Keywords note, method, property and constructor are local functions in New-PSClass and act as Class Definition by attaching objects to a PSClass Object returned by the CmdLet.
The PSClass has a New() method. Parameters to New are passed to the constructor script. Due to a bug in V1.0 of PowerShell, the Param statement does not work in the scriptblocks in PSClass. While I have not tried it yet, I have good reason to believe that it will work in V2.0. While it’s not as nice, the $Args variable works fine.
Instead of trying to describe all the syntax as small points in the blog entry, I’ll present an example Animal Class with a Dog and Bird Classes that inherit from it. While looking through it take note of:
· Private Notes use a –private switch. Within the class, scripts access them through a $private variable. Encapsulatioen isolates them from inherited classes and easy public access.
· Static Notes and Methods are attached to the PSClass object. This example shows a Static Note accessed in the Constructors. This may seem non-intuitive considering the Class Object used in the Constructor is not created until New-PSClass is completed. This is because the constructor script is not executed until an action Object is created using the New() method.
· While not demonstrated, private methods are supported and attached to the $private variable available in script execution.
· Note the use the –override switch on the ToString() method on the Dog Class. It can call methods on the base class through Invoke-BaseClassMethod
· Base function in constructors to call inherited constructors
$AnimalClass = New-PSClass Animal {
note -static ObjectCount 0
method -static DisplayObjectCount {
"$($this.ClassName) has $($this.ObjectCount) instances"
}
note -private Name
note -private Legs
constructor {
$private.Name = $Args[0]
$private.Legs = $Args[1]
$AnimalClass.ObjectCount += 1
property Name {
$private.Name
} -set {
Write-Host "Renaming $($this.Class.ClassName) '$($private.Name)' to '$($Args[0])'"
property Legs {
$private.Legs
method -override ToString {
"A $($this.Class.ClassName) named $($this.name) with $($this.Legs) Legs"
method Speak {
Throw "not implemented"
$DogClass = New-PSClass -inherit $AnimalClass Dog {
Base $Args[0] 4
$DogClass.ObjectCount += 1
"$(Invoke-BaseClassMethod 'ToString') with extra"
method -override Speak {
"Arf"
$BirdClass = New-PSClass -inherit $AnimalClass Bird {
Base $Args[0] 2
$BirdClass.ObjectCount += 1
"Squawk"
$Dog = $DogClass.New("Bowser")
$Dog.ToString()
$Dog.Name = "Duke"
$Dog.Speak()
$Bird = $BirdClass.New("Tweedy")
$Bird.ToString()
$Bird.Speak()
$DogClass.DisplayObjectCount()
$BirdClass.DisplayObjectCount()
$AnimalClass.DisplayObjectCount()
Output:
A Dog named Bowser with 4 Legs with extra
Renaming Dog 'Bowser' to 'Duke'
A Dog named Duke with 4 Legs with extra
Arf
A Bird named Tweedy with 2 Legs
Squawk
Dog has 1 instances
Bird has 1 instances
Animal has 2 instances
The cool thing about the implementation is it’s all in PowerShell. It’s a big chunk of code and I’m sure it’s not its final version, but it has a lot of cool techniques and some that should be fixed up. Also, I’ve been working on an XMS Serialization, Attribute support, and the ability to attach PSClass objects to any PowerShell object.
These PowerShell Scripts are in the attached zip file.
# ===================================================================================
# New-PSClass
# Object Oriented Scripting in Powershell
# http://www.cashfoley.com/
# Inspired by Bruce Payette's "Windows PowerShell in Action"
# Chapter 8 Script to add a CustomClass "keyword" to PowerShell
# http://manning.com/payette/
function New-PSClass
( [string] $ClassName = { Throw "ClassName required for New-PSClass"}
, [scriptblock] $definition = { Throw "Definition required for New-PSClass"}
, $Inherit
)
{
#======================================================================
# These Subfunctions are used in Class Definition Scripts
# - - - - - - - - - - - - - - - - - - - - - - - -
# Subfunction: constructor
# Assigns Constructor script to Class
function constructor ( [scriptblock] $script=$(Throw "Script is required for 'constructor'"))
if ($class.ConstructorScript)
Throw "Only one Constructor is allowed"
$class.ConstructorScript = $script
# Subfunction: note
# Adds Notes record to class if non-static
function note
param ( [string]$name={Throw "Note Name is Required"}
, $value=""
, [switch] $private
, [switch] $static
if ($static)
if ($private)
Throw "Private Static Notes are not supported"
Attach-PSNote $class $name $value
else
$class.Notes += @{Name=$name;DefaultValue=$value;Private=$private}
# Subfunction: method
# Add a method script to Class definition or
# attaches it to the Class if it is static
function method ([string]$name=$(Throw "Name is required for 'method'"), [scriptblock] $script=$(Throw "Script is required for 'method'"), [switch] $static, [switch]$private, [switch]$override )
Throw "Private Static Methods not supported"
Attach-PSScriptMethod $class $name $script
$class.Methods[$name] = @{Name=$name;Script=$script;Private=$private;Override=$override}
# Subfunction: property
# Add a property to Class definition or
function property ([string]$name, [scriptblock] $get=$(Throw "Get Script is required for 'property'"), [scriptblock] $set, [switch] $static, [switch]$private, [switch]$override )
Throw "Private Static Properties not supported"
Attach-PSProperty $class $name $get $set
$class.Properties[$name] = @{Name=$name;GetScript=$get;SetScript=$set;Private=$private;Override=$override}
$class = new-object Management.Automation.PSObject
# Class Internals
Attach-PSNote $class ClassName $ClassName
Attach-PSNote $class Notes @()
Attach-PSNote $class Methods @{}
Attach-PSNote $class Properties @{}
Attach-PSNote $class BaseClass $Inherit
Attach-PSNote $class ConstructorScript
Attach-PSNote $class PrivateName "__$($ClassName)_Private"
Attach-PSScriptMethod $class New {
$instance = new-object Management.Automation.PSObject
$this.AttachObject( $instance )
$this.Initialize( $instance, $Args )
$instance
Attach-PSScriptMethod $class Initialize {
if ($this.ConstructorScript -ne $null)
__PSClass-Initialize $Args[0] $Args[1]
Attach-PSScriptMethod $class AttachObject {
__PSClass-AttachObject $Args[0]
Attach-PSScriptMethod $class __LookupClassObject {
__PSClass-LookupClassObject $Args[0] $Args[1]
Attach-PSScriptMethod $class InvokeMethod {
__PSClass-InvokeMethod $Args[0] $Args[1] $Args[2]
Attach-PSScriptMethod $class InvokeProperty {
__PSClass-InvokePropertyMethod $Args[0] $Args[1] $Args[2] $Args[3]
# At last, execute definition script
&$definition
# return constructed class
$class
# These helper Cmdlets should only be called by New-PSClass. They exist to reduce
# the amount of code attached to each PSClass object. They rely on context
# variables not passed as parameters.
# __PSClass-Initialize
# Invokes Constructor Script and provides helper Base function to facilitate
# Inherited Constructors
function __PSClass-Initialize ($instance, $params)
function Base
if ($this.Class.BaseClass -eq $null)
Throw "No BaseClass implemented for $($this.Class.ClassName)"
$this.Class.BaseClass.Initialize($this,$Args)
trap {
$errorMsg = $_.Exception.Message + $_.Exception.ErrorRecord.InvocationInfo.PositionMessage
Throw $errorMsg
$_.ErrorDetails = $_.Exception.ErrorRecord.InvocationInfo.PositionMessage
$constructor = $this.ConstructorScript
$private = $Instance.($this.privateName)
$this = $instance
$constructor.InvokeReturnAsIs( $params )
# __PSClass-AttachObject
# Attaches Notes, Methods, and Properties to Instance Object
function __PSClass-AttachObject ($instance)
function AssurePrivate
if ($instance.($this.privateName) -eq $null)
Attach-PSNote $instance ($this.privateName) (new-object Management.Automation.PSObject)
Attach-PSNote $instance.($this.privateName) __Parent $instance
# Attach BaseClass
if ($this.BaseClass -ne $null)
$this.BaseClass.AttachObject($instance)
Attach-PSNote $instance Class $this
# Attach Notes
foreach ($note in $this.Notes)
if ($note.private)
AssurePrivate
Attach-PSNote $instance.($this.privateName) $note.Name $note.DefaultValue
Attach-PSNote $instance $note.Name $note.DefaultValue
# Attach Methods
foreach ($key in $this.Methods.keys)
$method = $this.Methods[$key]
$targetObject = $instance
# Private Methods are attached to the Private Object.
# However, when the script gets invoked, $this needs to be
# pointing to the instance object. $ObjectString resolves
# this for InvokeMethod
if ($method.private)
$targetObject = $instance.($this.privateName)
$ObjectString = '$this.__Parent'
$ObjectString = '$this'
# The actual script is not attached to the object. The Script attached to Object calls
# InvokeMethod on the Class. It looks up the script and executes it
$instanceScriptText = $ObjectString + '.Class.InvokeMethod( "' + $method.name + '", ' + $ObjectString + ', $Args )'
$instanceScript = $ExecutionContext.InvokeCommand.NewScriptBlock( $instanceScriptText )
Attach-PSScriptMethod $targetObject $method.Name $instanceScript -override:$method.override
# Attach Properties
foreach ($key in $this.Properties.keys)
$Property = $this.Properties[$key]
# Private Properties are attached to the Private Object.
if ($Property.private)
$instanceScriptText = $ObjectString + '.Class.InvokeProperty( "GET", "' + $Property.name + '", ' + $ObjectString + ', $Args )'
$getScript = $ExecutionContext.InvokeCommand.NewScriptBlock( $instanceScriptText )
if ($Property.SetScript -ne $null)
$instanceScriptText = $ObjectString + '.Class.InvokeProperty( "SET", "' + $Property.name + '", ' + $ObjectString + ', $Args )'
$setScript = $ExecutionContext.InvokeCommand.NewScriptBlock( $instanceScriptText )
$setScript = $null
Attach-PSProperty $targetObject $Property.Name $getScript $setScript -override:$Property.override
# __PSClass-LookupClassObject
# intended to look up methods and property objects on the Class. However,
# it can be used to look up any Hash Table entry on the class.
#
# if the object is not found on the instance class, it searches all Base Classes
# $ObjectType is the HashTable Member
# $ObjectName is the HashTable Key
# it returns the Class and Hashtable entry it was found in
function __PSClass-LookupClassObject ($ObjectType, $ObjectName)
$object = $this.$ObjectType[$ObjectName]
if ($object -ne $null)
$this
$object
$this.BaseClass.__LookupClassObject($ObjectType, $ObjectName)
# __PSClass-InvokeScript
# Used to invoke Method and Property scripts
# It adds an error handler so Script Info can be seen in the error
# It marshals $this and $private variables for the context of the script
# It provides a helper Invoke-BaseClassMethod for invoking base class methods
function __PSClass-InvokeScript ($class, $script, $object, $parms )
function Invoke-BaseClassMethod ($methodName, $parms)
Throw "$($this.Class.ClassName) does not have a BaseClass"
$class,$method = $this.Class.BaseClass.__LookupClassObject('Methods', $MethodName)
if ($method -eq $null)
Throw "Method $MethodName not defined for $className"
__PSClass-InvokeScript $class $method.Script $this $parms
$this = $object
$private = $this.($Class.privateName)
$script.InvokeReturnAsIs( $parms )
# __PSClass-InvokeMethod
# Script called by methods attached to instances. Looks up Method Script
# in instance class or in inherited class
function __PSClass-InvokeMethod($MethodName, $instance, $parms)
$class,$method = $this.__LookupClassObject('Methods', $MethodName)
Throw "Method $MethodName not defined for $($this.ClassName)"
__PSClass-InvokeScript $class $method.Script $instance $parms
# __PSClass-InvokePropertyMethod
# Script called by property scripts attached to instances. Looks up property Script
function __PSClass-InvokePropertyMethod ($PropertyType, $PropertyName, $instance, $parms)
$class,$property = $this.__LookupClassObject('Properties', $PropertyName)
if ($property -eq $null)
Throw "Property $PropertyName not defined for $($this.ClassName)"
if ($PropertyType -eq "GET")
__PSClass-InvokeScript $class $property.GetScript $instance $parms
__PSClass-InvokeScript $class $property.SetScript $instance $parms
function Attach-PSNote
param ( [PSObject]$object=$(Throw "Object is required")
, [string]$name=$(Throw "Note Name is Required")
, $value
if (! $object.psobject.members[$name])
$member = new-object management.automation.PSNoteProperty `
$name,$value
$object.psobject.members.Add($member)
if($value)
$object.$name = $value
function Attach-PSScriptMethod
, [string]$name=$(Throw "Method Name is Required")
, [scriptblock] $script
, [switch] $override
$member = new-object management.automation.PSScriptMethod `
$name,$script
if ($object.psobject.members[$name] -ne $null)
if ($override)
$object.psobject.members.Remove($name)
Throw "Method '$name' already exists with out 'override'"
function Attach-PSProperty
, [scriptblock] $get=$(Throw "get script is required")
, [scriptblock] $set
if ($set)
$scriptProperty = new-object management.automation.PsScriptProperty `
$name,$get,$set
$name,$get
if ( $object.psobject.properties[$name] -and $override)
$object.psobject.properties.Remove($name)
$object.psobject.properties.add($scriptProperty)
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.