gpt4 book ai didi

PowerShell 无法确定正在使用的参数集

转载 作者:行者123 更新时间:2023-12-03 10:03:11 26 4
gpt4 key购买 nike

我有一个带有四个参数的函数,其中两个可以设置也可以不设置。我正在尝试使用参数集来识别已设置的参数,但我在参数集解析方面遇到了一些问题。这些是四个可能的参数集 -

  • NonErrorObjectOrSplunkData
  • BothErrorObjectAndSplunkData
  • OnlyErrorObject
  • OnlySplunkData

  • 当我使用下面的示例代码测试所有四个参数集时, 的测试只有一个错误对象 只有一些 Splunk 数据 都失败并出现以下错误 -

    Test-ParameterSets : Parameter set cannot be resolved using the specified named parameters.



    从我的研究来看,这似乎失败了,因为 PowerSehll 无法确定参数集是否应该是 BothErrorObjectAndSplunkDataOnlyErrorObject/ 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 区分这些集合,如果只提供一个,或者两者都提供。

    根据大卫加德的评论编辑

    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

    关于PowerShell 无法确定正在使用的参数集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45189194/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com