我有一个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 }
}下面是这个cmdlet的帮助:
SYNTAX
Test-ParameterBinding -P1 <Int32> [-P2 <String>] [-P3 <Boolean>] [<Com…
Test-ParameterBinding -P2 <String> [<CommonParameters>]
Test-ParameterBinding -P3 <Boolean> [<CommonParameters>]查看代码和帮助,我会认为我可以像这样使用cmdlet:
Test-ParameterBinding -P2 'Bind to param set s2'
Test-ParameterBinding -P3 $true # Bind to param set s3但这两件事我都明白:
Parameter set cannot be resolved using the specified named parameters.问题1:在我的两种情况下, PowerShell应该能够绑定到参数集s2和s3吗?
这意味着没有时间为PowerShell的第2版实现它,或者他们没有发现这个问题。
问题2:,我的推理有什么问题吗?在这些情况下,参数绑定是否会失败?
我在PowerShell documentation中发现了一些可能与我的问题直接相关的东西
有一种情况是,即使指定了默认参数集名称,Windows PowerShell也不能使用默认参数集。Windows PowerShell运行库无法区分仅基于对象类型的参数集。例如,如果有一个参数集以字符串作为文件路径,而另一个参数集直接接受FileInfo对象,则PowerShell无法根据传递给cmdlet的值确定使用哪个参数集,也不使用默认参数集。在这种情况下,即使您指定了默认参数集名称,Windows PowerShell也会引发一条模糊的参数集错误消息。
发布于 2012-09-19 18:57:28
您的逻辑是正确的,Powershell应该能够根据您的函数定义和示例用法确定参数集。
显然,Powershell v2没有足够强大的逻辑来实现这一点。不过,它的工作方式与Powershell v3中预期的一样,这进一步证实了它在v2中是一个缺点/缺陷。
https://stackoverflow.com/questions/12500632
复制相似问题