简体   繁体   English

从脚本中声明时,函数的多个ParameterSets有效,但从模块中导入时,则不起作用

[英]Function's multiple ParameterSets work when being declared from a script, but not when being imported from a module

I wrote a 'wrapper' function for the Send-MailMessage function with a hard-coded SmtpServer argument (that's all it does). 我为带有硬编码的SmtpServer参数的Send-MailMessage函数编写了一个“包装器”函数(仅此而已)。 Here's the header declaring the sets: 这是声明集合的标题:

function SendMessage {
[CmdletBinding(DefaultParameterSetName='Path')]
Param(
    [Parameter(Mandatory,
               ParameterSetName='Path',
               HelpMessage='the path and filename to the message you would like to send')]
    [String]$Path,

    [Parameter(Mandatory,
               HelpMessage="A string containing the message which you want to send",
               ParameterSetName='Msg')]
    [String]$Msg,

    [Parameter(Mandatory, 
               HelpMessage='Your Admin Office 365 credentials',
               ParameterSetName='Path')]
    [Parameter(Mandatory,
               ParameterSetName='Msg')]
    [Alias('Credentials')]
    [System.Management.Automation.PSCredential]$Cred,

    [Parameter(Mandatory,
               HelpMessage='The address to which you want to send the message',
               ParameterSetName='Path')]
    [Parameter(Mandatory,
               ParameterSetName='Msg')]
    [String[]]$To,

    [Parameter(Mandatory,
               HelpMessage='The email from which you want to send the message',
               ParameterSetName='Path')]
    [Parameter(Mandatory,
               ParameterSetName='Msg')]
    [String]$From,

    [Parameter(Mandatory,
               HelpMessage='The subject of the email to send out',
               ParameterSetName='Path')]
    [Parameter(Mandatory,
               ParameterSetName='Msg')]
    [ValidateNotNullOrEmpty()]
    [String]$Subject,

    [Parameter(ParameterSetName='Path',
               HelpMessage='[Optional] If you want it cc''d to anyone')]
    [Parameter(ParameterSetName='Msg')]
    [String[]]$CC
)
# do function stuff
}

The function has two ParameterSet s: Path and Msg . 该函数具有两个ParameterSetPathMsg

When I run it as a .ps1 script, I can see both sets: 当我将其作为.ps1脚本运行时,可以看到两个集合:

PS U:\> test.ps1
PS U:\> gcm SendMessage -Syntax

SendMessage -Path <string> -Cred <pscredential> -To <string[]> -From <string> -Subject <string> [-CC <string[]>] [<CommonParameters>]

SendMessage -Msg <string> -Cred <pscredential> -To <string[]> -From <string> -Subject <string> [-CC <string[]>] [<CommonParameters>]

But when I import it as a module, I can only see the Path set: 但是,当我将其导入为模块时,只能看到“ Path集:

PS U:\> Import-Module -Name test.psm1
PS U:\> gcm SendMessage -Syntax

SendMessage [-Cred] <pscredential> [-Path] <string> [-To] <string> [-From] <string> [-Subject] <string> [[-CC] <string[]>] [<CommonParameters>]

PS U:\> 

I would like to be able to import both sets when I import the module. 导入模块时,我希望能够同时导入两个集合。

Is there anything obvious which I am missing? 有什么明显的我想念的吗? Thank you. 谢谢。

EDIT : I am using PS version 5.1, and have been testing using the ISE. 编辑 :我正在使用PS版本5.1,并已使用ISE进行测试。

Testing scripts and function with the ISE makes it so complex. 使用ISE测试脚本和功能使其变得如此复杂。 variables and functions in ISE is always available in global scope. ISE中的变量和函数始终在全局范围内可用。 Test this in PowerShell console. 在PowerShell控制台中对此进行测试。

Open a new PowerShell console, and dot source test.ps1 打开一个新的PowerShell控制台,并点源test.ps1

. c:\test.ps1
Get-Command SendMessage -Syntax

Open another PowerShell console and run Import-Module 打开另一个PowerShell控制台并运行Import-Module

Import-Module c:\Test.ps.1 -Verbose
Get-Command SendMessage -syntax

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从另一个Python安装导入的模块 - Module being imported from another Python install 无法识别PowerShell参数集 - PowerShell ParameterSets not being recognized 防止在重新运行脚本时删除由脚本固定在任务栏上的快捷方式 - Prevent shortcuts pinned to the taskbar by a script from being removed when re-running the script 从模块导出函数时,延迟绑定脚本块不起作用 - Delay-bind script block does not work when function is exported from module 来自导入模块的函数在 powershell 脚本中不可用 - Functions from an imported module are not available in a powershell script 当从另一个模块的函数创建时,隐式远程处理模块中的命令不可用 - Commands from implicit remoting module not available when created from another module's function 来自导入脚本的 Function 不执行 - Function from imported script doesn't execute 为什么 Pester 说这个函数被调用了 0 次,但我可以在运行脚本时确认它正在被调用? - Why does Pester say this function was called 0 times yet I can confirm it's being called when I run the script? 来自自定义模块的命令行开关未被识别 - Commandlets from a custom Module not being recognised 防止读取PowerShell脚本 - Prevent PowerShell script from being read
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM