I've got a module setup to be like a library for a few other scripts. I can't figure out how to get a class declaration into the script scope calling Import-Module
. I tried to arrange Export-Module
with a -class
argument, like the -function
, but there isn't a -class
available. Do I just have to declare the class in every script?
The setup:
import-module holidays
Here is what the class looks like:
Class data_block { $array $rows $cols data_block($a, $r, $c) { $this.array = $a $this.rows = $r $this.cols = $c } }
The Export-ModuleMember cmdlet specifies the module members that are exported from a script module ( . psm1 ) file, or from a dynamic module created by using the New-Module cmdlet. Module members include cmdlets, functions, variables, and aliases.
Import Classes using Using Module Statement The assembly-importing feature is a convenience; the module-importing feature is a requirement, because there's no other way to import classes from script modules. The Using statement has a module parameter that takes a module name string or a ModuleSpecification object.
The New-ModuleManifest cmdlet creates a new module manifest ( . psd1 ) file, populates its values, and saves the manifest file in the specified path. Module authors can use this cmdlet to create a manifest for their module.
The using statement allows you to specify which namespaces are used in the session. Adding namespaces simplifies usage of . NET classes and member and allows you to import classes from script modules and assemblies. The using statements must come before any other statements in a script or module.
PSA: There is a known issue that keeps old copies of classes in memory. It makes working with classes really confusing if you don't know about it. You can read about it here.
using
is Prone to PitfallsThe using
keyword is prone to various pitfalls as follows:
using
statement does not work for modules not in PSModulePath
unless you specify the module's full path in the using
statement. This is rather surprising because although a module is available via Get-Module
the using
statement may not work depending on how the module was loaded.using
statement can only be used at the very beginning of a "script". No combination of [scriptblock]::Create()
or New-Module
seems overcome this. A string passed to Invoke-Expression
seems to act as a sort of standalone script; a using
statement at the beginning of such a string sort of works. That is, Invoke-Expression "using module $path"
can succeed but the scope into which the contents of the module are made available seems rather inscrutable. For example, if Invoke-Expression "using module $path"
is used inside a Pester scriptblock, the classes inside the module are not available from the same Pester scriptblock.The above statements are based on this set of tests.
ScriptsToProcess
Prevents Access to Private Module FunctionsDefining a class in a script referred to by the module manifest's ScriptsToProcess
seems at first glance to export the class from the module. However, instead of exporting the class, it "creates the class in the global SessionState instead of the module's, so it...can't access private functions". As far as I can tell, using ScriptsToProcess
is like defining the class outside the module in the following manner:
# this is like defining c in class.ps1 and referring to it in ScriptsToProcess class c { [string] priv () { return priv } [string] pub () { return pub } } # this is like defining priv and pub in module.psm1 and referring to it in RootModule New-Module { function priv { 'private function' } function pub { 'public function' } Export-ModuleMember 'pub' } | Import-Module [c]::new().pub() # succeeds [c]::new().priv() # fails
Invoking this results in
public function priv : The term 'priv' is not recognized ... + [string] priv () { return priv } ...
The module function priv
is inaccessible from the class even though priv
is called from a class that was defined when that module was imported. This might be what you want, but I haven't found a use for it because I have found that class methods usually need access to some function in the module that I want to keep private.
.NewBoundScriptBlock()
Seems to Work ReliablyInvoking a scriptblock bound to the module containing the class seems to work reliably to export instances of a class and does not suffer from the pitfalls that using
does. Consider this module which contains a class and has been imported:
New-Module 'ModuleName' { class c {$p = 'some value'} } | Import-Module
Invoking [c]::new()
inside a scriptblock bound to the module produces an object of type [c]
:
PS C:\> $c = & (Get-Module 'ModuleName').NewBoundScriptBlock({[c]::new()}) PS C:\> $c.p some value
.NewBoundScriptBlock()
It seems that there is a shorter, idiomatic alternative to .NewBoundScriptBlock()
. The following two lines each invoke the scriptblock in the session state of the module output by Get-Module
:
& (Get-Module 'ModuleName').NewBoundScriptBlock({[c]::new()}) & (Get-Module 'ModuleName') {[c]::new()}}
The latter has the advantage that it will yield flow of control to the pipeline mid-scriptblock when an object is written to the pipeline. .NewBoundScriptBlock()
on the other hand collects all objects written to the pipeline and only yields once execution of the entire scriptblock has completed.
I found a way to load the classes without the need of "using module". In your MyModule.psd1 file use the line:
ScriptsToProcess = @('Class.ps1')
And then put your classes in the Class.ps1 file:
class MyClass {}
Update: Although you don't have to use "using module MyModule" with this method you still have to either:
Update2: This will load the Class to the current scope so if you import the Module from within a function for example the Class will not be accessible outside of the function. Sadly the only reliable method I see is to write your Class in C# and load it with Add-Type -Language CSharp -TypeDefinition 'MyClass...'
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With