5

我正在编写一个函数,其中两个参数应该是独占的可选的。

以下是有效输入:

new-event -Title sometitle -Text sometext -TimestampHappened 1234567 -SomeOtherOptionalParam somestring

new-event -Title sometitle -Text sometext -DateHappened (get-date) -SomeOtherOptionalParam somestring

new-event -Title sometitle -Text sometext -SomeOtherOptionalParam somestring

new-event -Title sometitle -Text sometext

这是一个无效的输入:

new-event -Title sometitle -Text sometext -DateHappened (get-date) -TimestampHappened 1234567 -SomeOtherOptionalParam somestring

到目前为止,这是我的代码:

[CmdletBinding()]

# Most parameters belong to Default, New-Event:ByDate and New-Event:ByTimestamp parameter sets
param (
     [Parameter(
        Position=0,
        Mandatory=$True,
        ParameterSetName="Default"
    )]
    [Parameter(
        Position=0,
        Mandatory=$True,
        ParameterSetName="New-Event:ByDate"
    )]
    [Parameter(
        Position=0,
        Mandatory=$True,
        ParameterSetName="New-Event:ByTimestamp"
    )]
    [ValidateNotNullOrEmpty()]
    [String]$Title,

[Parameter(
        Position=1,
        Mandatory=$True,
        ParameterSetName="Default"
    )]
    [Parameter(
        Position=1,
        Mandatory=$True,
        ParameterSetName="New-Event:ByDate"
    )]
    [Parameter(
        Position=1,
        Mandatory=$True,
        ParameterSetName="New-Event:ByTimestamp"
    )]
    [ValidateNotNullOrEmpty()]
    [String]$Text,

 [Parameter(
        Position=2,
        Mandatory=$False,
        ParameterSetName="New-Event:ByDate"
    )]
    [ValidateNotNullOrEmpty()]
    [datetime]$DateHappened,

[Parameter(
    Position=2,
    Mandatory=$False,
    ParameterSetName="New-Event:ByTimestamp"
    )]
    [ValidateNotNullOrEmpty()]
    [Double]$TimestampHappened,


   [Parameter(
        Position=3,
        Mandatory=$False,
        ParameterSetName="Default"
    )]
   [Parameter(
        Position=3,
        Mandatory=$False,
        ParameterSetName="New-Event:ByDate"
    )]
    [Parameter(
        Position=3,
        Mandatory=$False,
        ParameterSetName="New-Event:ByTimestamp"
    )]
    [String]$SomeOtherParam,
    ...

这是我打电话时得到的Get-Help

PS> get-help New-Event

NAME
    New-Event

SYNOPSIS
    Post an event to the stream.


SYNTAX
    New-Event [-Title] <String> [-Text] <String> [[-TimestampHappened] <Double>] [[-Priority] <String>] [[-Hostname] <String>] [[-Tags] <String[]>] [[-AlertType] <String>] [<CommonParameters>]

    New-Event [-Title] <String> [-Text] <String> [[-DateHappened] <DateTime>] [[-Priority] <String>] [[-Hostname] <String>] [[-Tags] <String[]>] [[-AlertType] <String>]  <String>] [<CommonParameters>]

    New-Event [-Title] <String> [-Text] <String> [[-Priority] <String>] [[-Hostname] <String>] [[-Tags] <String[]>] [[-AlertType] <String>] [<CommonParameters>]

但是,当我尝试仅使用两个强制参数调用函数时,出现以下错误:

New-Event -Title test -Text text
New-Event : Parameter set cannot be resolved using the specified named parameters.
At line:1 char:1
+ New-Event -Title test -Text text
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [New-Event], ParameterBindingException
    + FullyQualifiedErrorId : AmbiguousParameterSet,New-Event

我在这里遗漏了一些东西,但我不知道是什么......

如何获得两个互斥且可选的参数?

4

1 回答 1

6

这很有意义。您有 3 个参数集,每个集都包含 2 个强制参数。PowerShell 如何确定您打算使用哪个集合?

幸运的是,该[CmdletBinding()]属性可以采用一个有助于这种确切情况的参数:DefaultParameterSetName. 设置此项允许 PowerShell 在(某些)歧义的情况下使用此设置。像这样使用它:

[CmdletBinding(DefaultParameterSetName='Default')]

请注意,在这种情况下,将其命名为默认值;它可以被命名为任何东西。

于 2017-02-11T00:51:42.467 回答