【发布时间】:2015-02-10 11:21:24
【问题描述】:
我有以下代码:
Function SCCE {
[CmdletBinding()]
Param(
[Parameter(Position=0, Mandatory=$True, ValueFromPipeLine=$True,ParameterSetName="Files")]
[System.IO.FileInfo[]]$sourceFiles,
[Parameter(Position=0, Mandatory=$True, ValueFromPipeLine=$True, ParameterSetName='File')]
[System.IO.FileInfo]$sourceFile
)
PROCESS {
Write-Host $PSCmdlet.ParameterSetName
}
}
我希望对指定的类型进行这项工作,据我所知,即使类型是准确的,它也不行。
例如:
$file = (ls)[0]
$file.getType() #FileInfo
SCCE $file
SCCE : 无法使用指定的命名解析参数集 参数。在 line:1 char:1
+ SCCE $文件
+ ~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [SCCE], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : AmbiguousParameterSet,SCCE
注释掉 sourceFiles 参数似乎解决了问题,但我不明白为什么这是模棱两可的。与第二个参数完美匹配。
我该如何解决这个问题?
【问题讨论】:
-
您是否尝试指定默认参数集?
-
@PetSerAl 嗯,不。不过,这似乎奏效了。有没有办法解决核心问题(Powershell 正在寻找一个精确匹配的近似匹配)?
-
由于我从未见过描述 PowerShell 参数集解析的确切行为的文档,因此指定默认参数集是我能提供的最佳选择。