PowerShell 无法确定正在使用哪个参数集
PowerShell unable the determine which Parameter Set is in use
我有一个有四个参数的函数,其中两个可以设置也可以不设置。我正在尝试使用参数集来识别已设置的参数,但我在参数集解析方面遇到了一些问题。这些是四个可能的参数集 -
- NeitherErrorObjectOrSplunkData
- ErrorObjectAndSplunkData
- OnlyErrorObject
- 仅 SplunkData
当我使用下面的示例代码测试所有四个参数集时,只有一个错误对象和只有一些 Splunk 数据的测试都失败并出现以下错误 -
Test-ParameterSets : Parameter set cannot be resolved using the specified named parameters.
根据我的研究,这似乎失败了,因为 PowerSehll 无法确定参数集应该是 BothErrorObjectAndSplunkData
还是 OnlyErrorObject
/OnlySplunkData
,我可以理解。
如何更改我的代码以确定正在使用上述哪些参数集?如果参数集根本不可能,我还能如何实现我的目标?
示例代码
function Test-ParameterSets
{
[CmdLetBinding(DefaultParameterSetName="NeitherErrorObjectOrSplunkData")]
param(
[Parameter(Mandatory=$true)]
[Parameter(ParameterSetName="NeitherErrorObjectOrSplunkData")]
[Parameter(ParameterSetName="BothErrorObjectAndSplunkData")]
[parameter(ParameterSetName="OnlyErrorObject")]
[Parameter(ParameterSetName="OnlySplunkData")]
[String]$Message,
[parameter(ValueFromPipeline=$true)]
[Parameter(ParameterSetName="BothErrorObjectAndSplunkData")]
[parameter(ParameterSetName="OnlyErrorObject")]
[Object]$ErrorObject,
[Parameter(ParameterSetName="BothErrorObjectAndSplunkData")]
[Parameter(ParameterSetName="OnlySplunkData")]
[String[]]$SplunkData,
[ValidateSet("ERROR", "WARN", "INFO")]
[String]$Severity = "ERROR"
)
Write-Host "$message -"
Write-Host "-- Parameter Set: $($PSCmdlet.ParameterSetName)`n"
return
}
Test-ParameterSets -Message "Neither an error object or an Splunk data"
Test-ParameterSets -Message "Only an error object" -ErrorObject (New-Object -TypeName PSCustomObject)
Test-ParameterSets -Message "Only some Splunk data" -SplunkData "Test"
Test-ParameterSets -Message "Both an error object and Splunk data" -ErrorObject (New-Object -TypeName PSCustomObject) -SplunkData "Test"
示例代码输出
Neither an error object or an Splunk data -
-- Parameter Set: NeitherErrorObjectOrSplunkData
Test-ParameterSets : Parameter set cannot be resolved using the specified named parameters.
At line:31 char:1
+ Test-ParameterSets -Message "Only an error object" -ErrorObject (New- ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Test-ParameterSets], ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,Test-ParameterSets
Test-ParameterSets : Parameter set cannot be resolved using the specified named parameters.
At line:32 char:1
+ Test-ParameterSets -Message "Only some Splunk data" -SplunkData "Test ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Test-ParameterSets], ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,Test-ParameterSets
Both an error object and Splunk data -
-- Parameter Set: BothErrorObjectAndSplunkData
指定 $ErrorObject
和 $SplunkData
变量对于这些集合是必需的。
这将允许 PowerShell 在只提供一个集合或同时提供两个集合的情况下区分这些集合。
根据 David Gard 的评论进行编辑
function Test-ParameterSets
{
[CmdLetBinding(DefaultParameterSetName="NeitherErrorObjectOrSplunkData")]
param(
# edit: removed ParameterSetName as belong to all sets so redundant
[Parameter(Mandatory=$true)]
[String]$Message,
# edit: included ValueFromPipeline in the same Parameter declarations
# as ParameterSetName
[Parameter(ValueFromPipeline=$true, Mandatory=$true, ParameterSetName="BothErrorObjectAndSplunkData")]
[parameter(ValueFromPipeline=$true, Mandatory=$true, ParameterSetName="OnlyErrorObject")]
[Object]$ErrorObject,
[Parameter(Mandatory=$true, ParameterSetName="BothErrorObjectAndSplunkData")]
[Parameter(Mandatory=$true, ParameterSetName="OnlySplunkData")]
[String[]]$SplunkData,
[ValidateSet("ERROR", "WARN", "INFO")]
[String]$Severity = "ERROR"
)
Write-Host "$message -"
Write-Host "-- Parameter Set: $($PSCmdlet.ParameterSetName)`n"
return
}
Test-ParameterSets -Message "Neither an error object or an Splunk data"
Test-ParameterSets -Message "Only an error object" -ErrorObject (New-Object -TypeName PSCustomObject)
Test-ParameterSets -Message "Only some Splunk data" -SplunkData "Test"
Test-ParameterSets -Message "Both an error object and Splunk data" -ErrorObject (New-Object -TypeName PSCustomObject) -SplunkData "Test"
输出
Neither an error object or an Splunk data -
-- Parameter Set: NeitherErrorObjectOrSplunkData
Only an error object -
-- Parameter Set: OnlyErrorObject
Only some Splunk data -
-- Parameter Set: OnlySplunkData
Both an error object and Splunk data -
-- Parameter Set: BothErrorObjectAndSplunkData
我有一个有四个参数的函数,其中两个可以设置也可以不设置。我正在尝试使用参数集来识别已设置的参数,但我在参数集解析方面遇到了一些问题。这些是四个可能的参数集 -
- NeitherErrorObjectOrSplunkData
- ErrorObjectAndSplunkData
- OnlyErrorObject
- 仅 SplunkData
当我使用下面的示例代码测试所有四个参数集时,只有一个错误对象和只有一些 Splunk 数据的测试都失败并出现以下错误 -
Test-ParameterSets : Parameter set cannot be resolved using the specified named parameters.
根据我的研究,这似乎失败了,因为 PowerSehll 无法确定参数集应该是 BothErrorObjectAndSplunkData
还是 OnlyErrorObject
/OnlySplunkData
,我可以理解。
如何更改我的代码以确定正在使用上述哪些参数集?如果参数集根本不可能,我还能如何实现我的目标?
示例代码
function Test-ParameterSets
{
[CmdLetBinding(DefaultParameterSetName="NeitherErrorObjectOrSplunkData")]
param(
[Parameter(Mandatory=$true)]
[Parameter(ParameterSetName="NeitherErrorObjectOrSplunkData")]
[Parameter(ParameterSetName="BothErrorObjectAndSplunkData")]
[parameter(ParameterSetName="OnlyErrorObject")]
[Parameter(ParameterSetName="OnlySplunkData")]
[String]$Message,
[parameter(ValueFromPipeline=$true)]
[Parameter(ParameterSetName="BothErrorObjectAndSplunkData")]
[parameter(ParameterSetName="OnlyErrorObject")]
[Object]$ErrorObject,
[Parameter(ParameterSetName="BothErrorObjectAndSplunkData")]
[Parameter(ParameterSetName="OnlySplunkData")]
[String[]]$SplunkData,
[ValidateSet("ERROR", "WARN", "INFO")]
[String]$Severity = "ERROR"
)
Write-Host "$message -"
Write-Host "-- Parameter Set: $($PSCmdlet.ParameterSetName)`n"
return
}
Test-ParameterSets -Message "Neither an error object or an Splunk data"
Test-ParameterSets -Message "Only an error object" -ErrorObject (New-Object -TypeName PSCustomObject)
Test-ParameterSets -Message "Only some Splunk data" -SplunkData "Test"
Test-ParameterSets -Message "Both an error object and Splunk data" -ErrorObject (New-Object -TypeName PSCustomObject) -SplunkData "Test"
示例代码输出
Neither an error object or an Splunk data -
-- Parameter Set: NeitherErrorObjectOrSplunkDataTest-ParameterSets : Parameter set cannot be resolved using the specified named parameters.
At line:31 char:1
+ Test-ParameterSets -Message "Only an error object" -ErrorObject (New- ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Test-ParameterSets], ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,Test-ParameterSetsTest-ParameterSets : Parameter set cannot be resolved using the specified named parameters.
At line:32 char:1
+ Test-ParameterSets -Message "Only some Splunk data" -SplunkData "Test ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Test-ParameterSets], ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,Test-ParameterSetsBoth an error object and Splunk data -
-- Parameter Set: BothErrorObjectAndSplunkData
指定 $ErrorObject
和 $SplunkData
变量对于这些集合是必需的。
这将允许 PowerShell 在只提供一个集合或同时提供两个集合的情况下区分这些集合。
根据 David Gard 的评论进行编辑
function Test-ParameterSets
{
[CmdLetBinding(DefaultParameterSetName="NeitherErrorObjectOrSplunkData")]
param(
# edit: removed ParameterSetName as belong to all sets so redundant
[Parameter(Mandatory=$true)]
[String]$Message,
# edit: included ValueFromPipeline in the same Parameter declarations
# as ParameterSetName
[Parameter(ValueFromPipeline=$true, Mandatory=$true, ParameterSetName="BothErrorObjectAndSplunkData")]
[parameter(ValueFromPipeline=$true, Mandatory=$true, ParameterSetName="OnlyErrorObject")]
[Object]$ErrorObject,
[Parameter(Mandatory=$true, ParameterSetName="BothErrorObjectAndSplunkData")]
[Parameter(Mandatory=$true, ParameterSetName="OnlySplunkData")]
[String[]]$SplunkData,
[ValidateSet("ERROR", "WARN", "INFO")]
[String]$Severity = "ERROR"
)
Write-Host "$message -"
Write-Host "-- Parameter Set: $($PSCmdlet.ParameterSetName)`n"
return
}
Test-ParameterSets -Message "Neither an error object or an Splunk data"
Test-ParameterSets -Message "Only an error object" -ErrorObject (New-Object -TypeName PSCustomObject)
Test-ParameterSets -Message "Only some Splunk data" -SplunkData "Test"
Test-ParameterSets -Message "Both an error object and Splunk data" -ErrorObject (New-Object -TypeName PSCustomObject) -SplunkData "Test"
输出
Neither an error object or an Splunk data -
-- Parameter Set: NeitherErrorObjectOrSplunkData
Only an error object -
-- Parameter Set: OnlyErrorObject
Only some Splunk data -
-- Parameter Set: OnlySplunkData
Both an error object and Splunk data -
-- Parameter Set: BothErrorObjectAndSplunkData