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

时间:2018-05-11 17:45:39

标签: powershell powershell-v5.0

我写了一个'包装'具有硬编码SmtpServer参数的Send-MailMessage函数的函数(它完全是这样)。这是标识集合的标题:

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
}

该函数有两个ParameterSet s:PathMsg

当我将其作为.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>]

但是当我将其作为模块导入时,我只能看到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:\> 

我希望能够在导入模块时导入两个集。

有什么明显的遗漏吗? 谢谢。

编辑:我使用的是PS 5.1版,并且一直在使用ISE进行测试。

1 个答案:

答案 0 :(得分:0)

使用ISE测试脚本和功能使其变得如此复杂。 ISE中的变量和函数始终可在全局范围内使用。在PowerShell控制台中测试它。

打开一个新的PowerShell控制台,点源test.ps1

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

打开另一个PowerShell控制台并运行Import-Module

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