[英]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
. 该函数具有两个
ParameterSet
: Path
和Msg
。
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.