Skip to content

Instantly share code, notes, and snippets.

@tophatsteve
Created June 7, 2012 12:55
Show Gist options
  • Save tophatsteve/2888674 to your computer and use it in GitHub Desktop.
Save tophatsteve/2888674 to your computer and use it in GitHub Desktop.
Powershell class factory
function ClassFactory(){
$object = New-Module {
$myField = 5
function xTimesMyField($x) {
$x * $myField
}
Export-ModuleMember -Variable * -Function *
} -asCustomObject
return $object
}
# create object
$obj = ClassFactory
# add method to object
Add-Member -InputObject $obj -MemberType ScriptMethod -Name "sayHello" -Value { echo "Hello" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment