执行脚本时出现Powershell“无法解析参数集”错误

时间:2019-12-11 11:49:30

标签: powershell parameter-sets

我有一个带有相当长的参数集的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

几个月来,我一直在想问题可能出在什么地方,而我一直找不到解决方案。谁能指出一个提示来帮助我解决问题?

非常感谢您。 问候。

1 个答案:

答案 0 :(得分:0)

在第一条评论之后,我从该网页复制了代码,并将其粘贴到我的脚本中,并且工作正常。我将脚本与SVN存储库中的先前版本进行了比较,并且应用于$ Reference参数的参数集名称“ OnlyListing”存在差异。显然,两个参数集名称是相同的,但是其中一个包含一个隐藏字符。

谢谢大家的帮助。