Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell module initialization

Does PowerShell call any initialization code when a module is loaded?

I am looking for something like a Perl BEGIN block, or a constructor.

Both NEW-MODULE and IMPORT-MODULE will return a PSCustomObject. I am trying to encapsulate a custom object in a module to avoid lengthy code in scripts. One method that tests well in open code is:

$m = new-module -scriptblock {
New-Object PSCustomObject | 
    Add-Member NoteProperty -name person -value Frodo -passthru | 
    Add-Member ScriptMethod Who { $this.person }  -passthru |
    Add-Member ScriptMethod Mod { 
        param($x)
        $this.person = $x
        } -passthru
} -ascustomobject -returnresult

Ideally I would like to drop this code into a module and use something like:

$MyObj = Import-Module -Name ".\MyPackage" -AsCustomObject

and have MyObj be a handle to an object the same as the first snippet provides.

Suggestions appreciated.

like image 676
James Page Avatar asked Jul 12 '13 23:07

James Page


2 Answers

It's not clear if you want to run initialization code when a module is loaded (like Perl's BEGIN block) or if you want to create a custom class (which is what "constructor" suggests).

Initialization code in a module is easy. Any code in a module not embedded in a function is executed when the module is imported.

Creating a custom class isn't supported natively in PS. But see: http://psclass.codeplex.com/. You can also write C#, VBScript, etc. and use Add-Type.

Import-module won't work to simulate a class, because you can only have 1 instance of a module with a given name - at best you'd have a singleton class. (BTW, import-module does have a -passthru parameter, which would more or less make your last line of code work - as a singleton. You'd also have to add export-module -variable * -function * to your module code) You could use New-Module to simulate a class though. And you could wrap it in a function named, new-myClass for example.

BTW, if you use the -ASCustomObject parameter you end up with a hashtable, which doesn't support "this" (in words hash table values that are script blocks don't have a built-in way to refer to the hashtable itself). If you use new-module without -AsCustomObject (and potentially use a factory function, for example new-myclass) then you could simulate "this.varInMyModule" with & $myModule $varInMyModule. However if you create a PSCustomObject, using Add-Member, then script method have access to $this and it in general acts a lot more like a typical object with properties and methods.

like image 197
Χpẘ Avatar answered Sep 20 '22 02:09

Χpẘ


Modules are really supposed to output cmdlets, not objects. A module should provide a set of related cmdlets. There is a way to send data into the module using Import-Modules's -ArgumentList parameter as show here. You could use the technique to provide a server name for your cmdlets to connect to for example. The PowerCLI module handles that differently using a cmdlet that creates a script scope connection object ($script:connection) that the other cmdlets check for and re-use if it exists similar to this:

#test.psm1
$script:myvar = "hi"
function Show-MyVar {Write-Host $script:myvar}
function Set-MyVar ($Value) {$script:myvar = $Value}
#end test.psm1
like image 27
Andy Arismendi Avatar answered Sep 19 '22 02:09

Andy Arismendi