0

我有一个带有相当长参数集的 powershell 脚本:

Param(
    [Parameter(Position = 0, Mandatory = $true, ParameterSetName = 'Senders')]
    [Parameter(Position = 0, Mandatory = $true, ParameterSetName = 'Recipients')]
    [Parameter(Position = 0, Mandatory = $true, ParameterSetName = 'Global')]
    [Parameter(Position = 0, Mandatory = $true, ParameterSetName = 'Manager')]
    [Parameter(Position = 0, Mandatory = $true, ParameterSetName = 'CatalogueName')]
    [Parameter(Position = 0, Mandatory = $true, ParameterSetName = 'ReapplyPermissions')]
    [Parameter(Position = 0, Mandatory = $true, ParameterSetName = 'OnlyListing')]
    [String]$SharedElement,

    [Parameter(Mandatory = $true, ParameterSetName = 'Senders')]
    [String]$Senders,

    [Parameter(Mandatory = $false, ParameterSetName = 'Senders')]
    [Parameter(Mandatory = $true, ParameterSetName = 'Recipients')]
    [String]$Recipients,

    [Parameter(Mandatory = $true, ParameterSetName = 'Global')]
    [String]$Global,

    [Parameter(Mandatory = $false, ParameterSetName = 'Senders')]
    [Parameter(Mandatory = $false, ParameterSetName = 'Recipients')]
    [Parameter(Mandatory = $false, ParameterSetName = 'Global')]
    [Parameter(Mandatory = $true, ParameterSetName = 'Manager')]
    [Alias("Owner")]
    [String]$Manager,

    [Parameter(Mandatory = $false, ParameterSetName = 'Senders')]
    [Parameter(Mandatory = $false, ParameterSetName = 'Recipients')]
    [Parameter(Mandatory = $false, ParameterSetName = 'Global')]
    [Parameter(Mandatory = $false, ParameterSetName = 'Manager')]
    [Parameter(Mandatory = $true, ParameterSetName = 'CatalogueName')]
    [String]$CatalogueName,

    [Parameter(Mandatory = $true, ParameterSetName = 'ReapplyPermissions')]
    [Switch]$ReapplyPermissions,

    [Parameter(Mandatory = $true, ParameterSetName = 'OnlyListing')]
    [Switch]$OnlyListing,

    [Parameter(Mandatory = $false, ParameterSetName = 'Senders')]
    [Parameter(Mandatory = $false, ParameterSetName = 'Recipients')]
    [Parameter(Mandatory = $false, ParameterSetName = 'Global')]
    [Parameter(Mandatory = $false, ParameterSetName = 'Manager')]
    [Parameter(Mandatory = $false, ParameterSetName = 'CatalogueName')]
    [Parameter(Mandatory = $false, ParameterSetName = 'ReapplyPermissions')]
    [Parameter(Mandatory = $false, ParameterSetName = 'OnlyListing')]
    [Switch]$NoLog,

    [Parameter(Mandatory = $false, ParameterSetName = 'Senders')]
    [Parameter(Mandatory = $false, ParameterSetName = 'Recipients')]
    [Parameter(Mandatory = $false, ParameterSetName = 'Global')]
    [Parameter(Mandatory = $false, ParameterSetName = 'Manager')]
    [Parameter(Mandatory = $false, ParameterSetName = 'CatalogueName')]
    [Parameter(Mandatory = $false, ParameterSetName = 'ReapplyPermissions')]
    [Parameter(Mandatory = $false, ParameterSetName = 'OnlyListing')]
    [String]$Reference,

    [Parameter(Mandatory = $false, ParameterSetName = 'Senders')]
    [Parameter(Mandatory = $false, ParameterSetName = 'Recipients')]
    [Parameter(Mandatory = $false, ParameterSetName = 'Global')]
    [Parameter(Mandatory = $false, ParameterSetName = 'Manager')]
    [Parameter(Mandatory = $false, ParameterSetName = 'CatalogueName')]
    [Parameter(Mandatory = $false, ParameterSetName = 'ReapplyPermissions')]
    [Parameter(Mandatory = $false, ParameterSetName = 'OnlyListing')]
    [Switch]$AskCredentials,

    [Parameter(Mandatory = $true, ParameterSetName = 'Help')]
    [Switch]$Help
)

我已经根据我的需要测试了这些参数的功能,它工作正常,除了在一种特殊情况下:当我使用参数 SharedElement、OnlyListing 和 Reference 运行脚本时

script.ps1 -SharedElement email.address@domain.com -OnlyListing -Reference "Some string"

我总是得到一个错误:

无法使用指定的命名参数解析参数集

但是,当我使用参数 SharedElement、OnlyListing 和 NoLog 运行脚本时,它可以正常工作:

script.ps1 -SharedElement email.address@domain.com -OnlyListing -NoLog

几个月来我一直想知道问题可能是什么,但我一直无法找到解决方案。谁能指出帮助我解决问题的提示?

非常感谢您提前。问候。

4

1 回答 1

0

在第一条评论之后,我从这个网页复制了代码,将其粘贴到我的脚本中,它运行良好。我将该脚本与我的 SVN 存储库中的先前版本进行了比较,应用到 $Reference 参数的参数集名称“OnlyListing”有所不同。显然,两个参数集名称相同,但其中一个包含隐藏字符。

谢谢大家的帮助。

于 2019-12-11T20:47:51.457 回答