从模块导出Powershell 5枚举声明 [英] Export Powershell 5 enum declaration from a Module

查看:58
本文介绍了从模块导出Powershell 5枚举声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在模块内定义了一个枚举类型。加载模块后,如何导出它以从外部进行访问?

I have an enum type defined within a module. How do I export it to be accessible from outside once the module has been loaded?

enum fruits {
 apple
 pie
}

function new-fruit {
    Param(
        [fruits]$myfruit
    )
    write-host $myfruit
}

我的高级函数采用枚举,而不是 ValidateSet ,如果枚举可用,则可以使用,但是

My advanced function takes the enum instead of the ValidateSet which works if the enum is available, but fails if it isn't.

更新:
将其分离为ps1并进行点源(ScriptsToProcess)有效,但是,我希望有一种更清洁的方法。

Update: Separating it into a ps1 and dot-sourcing it (ScriptsToProcess) works, however I would wish that there's a cleaner way.

推荐答案

您可以在使用<$ c $加载模块后访问枚举。 c>正在使用模块... 命令。

例如:

MyModule .psm1

MyModule.psm1

enum MyPriority {
    Low = 0
    Medium = 1
    high = 2
}
function Set-Priority {
  param(
    [Parameter(HelpMessage = 'Priority')] [MyPriority] $priority
  )
  Write-Host $Priority
}  
Export-ModuleMember -function Set-Priority

制作:

New-ModuleManifest MyModule.psd1 -RootModule 'MyModule.psm1' -FunctionsToExport '*' 

然后在Powershell中...

Then in Powershell...

Import-Module .\MyModule\MyModule.psd1
PS C:\Scripts\MyModule> [MyPriority] $p = [MyPriority ]::High
Unable to find type [MyPriority].
At line:1 char:1
+ [MyPriority] $p = [MyPriority ]::High
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (MyPriority:TypeName) [], RuntimeException
    + FullyQualifiedErrorId : TypeNotFound

PS C:\Scripts\MyModule> using module .\MyModule.psd1
PS C:\Scripts\MyModule> [MyPriority] $p = [MyPriority ]::High
PS C:\Scripts\MyModule> $p
high

这篇关于从模块导出Powershell 5枚举声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆