in a script I try to use an enum that is defined in a module. Here's the script Sample.ps1:
using module SampleModule
param(
[SampleEnum] $MyEnum
)
$MyEnum
and here's SampleModule.psm1:
enum SampleEnum {
A
B
C
}
Starting Sample.ps1 from a fresh Powershell gives me the following error:
PS C:\test> .\Sample.ps1
Unable to find type [SampleEnum].
At C:\test\Sample.ps1:3 char:2
+ [SampleEnum] $MyEnum
+ ~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (SampleEnum:TypeName) [], RuntimeException
+ FullyQualifiedErrorId : TypeNotFound
I can explicitly import the module from the shell like this:
PS C:\test> using module SampleModule
PS C:\test> .\Sample.ps1
PS C:\test> .\Sample.ps1 A
A
...but of course I want the script to be self-contained. Why does the error occur and how to work-around it?
Update to be more specific: Using the enum in the script body - not in the param declaration - does work, like here:
using module SampleModule
$MyEnum = [SampleEnum]::A
$MyEnum
Using Import-Module does not work in either scripts, it simply does not make the enum visible (see e.g. enter link description here).
So my problem is to use the enum in the param of the script.