简体   繁体   English

PowerShell中的参数绑定问题

[英]Parameter binding issue in PowerShell

I have this PowerShell cmdlet: 我有这个PowerShell cmdlet:

function Test-ParameterBinding {
    #
    # .SYNOPSIS
    # Tests parameter binding.
    #
    [CmdletBinding()]
    param (    

        [Parameter(ParameterSetName = 's1', Mandatory = $true)]
        [int] $P1,

        [Parameter(ParameterSetName = 's1')]
        [Parameter(ParameterSetName = 's2', Mandatory = $true)]
        [string] $P2,

        [Parameter(ParameterSetName = 's1')]
        [Parameter(ParameterSetName = 's3', Mandatory = $true)]
        [bool] $P3
    )
    process { $PSCmdlet }
}

And here is the help for this cmdlet: 以下是此cmdlet的帮助:

SYNTAX
    Test-ParameterBinding -P1 <Int32> [-P2 <String>] [-P3 <Boolean>] [<Com…

    Test-ParameterBinding -P2 <String> [<CommonParameters>]

    Test-ParameterBinding -P3 <Boolean> [<CommonParameters>]

Looking at the code and help I would think I could use the cmdlet like this: 查看代码和帮助我认为我可以像这样使用cmdlet:

Test-ParameterBinding -P2 'Bind to param set s2'
Test-ParameterBinding -P3 $true # Bind to param set s3

But for both of these I get: 但对于这两个我得到:

Parameter set cannot be resolved using the specified named parameters.

Question 1: Should PowerShell be able to bind to parameter sets s2 and s3 in my two cases? 问题1: PowerShell能否在我的两种情况下绑定到参数集s2s3

That means that there was not time to implement it for version 2 of PowerShell, or they did not uncover this issue. 这意味着没有时间为PowerShell的第2版实现它,或者他们没有发现这个问题。

Question 2: Is there something wrong with my reasoning here? 问题2:我的推理在这里有什么问题吗? Should parameter binding fail in these cases? 参数绑定在这些情况下是否会失败?


I found something that may be directly related to my issue here in the PowerShell documentation : 我在PowerShell文档中找到了可能与我的问题直接相关的内容

There is one case where Windows PowerShell cannot use the default parameter set even if a default parameter set name is specified. 有一种情况是,即使指定了默认参数集名称,Windows PowerShell也无法使用默认参数集。 The Windows PowerShell runtime cannot distinguish between parameter sets based solely on object type. Windows PowerShell运行时无法仅基于对象类型区分参数集。 For example, if you have one parameter set that takes a takes a string as the file path, and another set that takes a FileInfo object directly, Windows PowerShell cannot determine which parameter set to use based on the values passed to the cmdlet, nor does it use the default parameter set. 例如,如果您有一个参数集,其中取一个字符串作为文件路径,另一个集合直接接受FileInfo对象,则Windows PowerShell无法根据传递给cmdlet的值确定要使用的参数集,也不能它使用默认参数集。 In this case, even if you specify a default parameter set name, Windows PowerShell throws an ambiguous parameter set error message. 在这种情况下,即使您指定了默认参数集名称,Windows PowerShell也会抛出不明确的参数集错误消息。

Your logic is correct, Powershell should be able to figure out the parameter set based on your function definition and example usages. 你的逻辑是正确的,Powershell 应该能够根据你的函数定义和示例用法找出参数集。

Apparently Powershell v2 simply did not have robust enough logic for this. 显然,Powershell v2对此没有足够强大的逻辑。 It works as expected in Powershell v3, though, which is further confirmation that it's a shortcoming/bug in v2. 虽然它在Powershell v3中按预期工作,这进一步证实了它是v2中的缺点/错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM