PowerShell:DynamicParam:获取传递参数的列表?

PowerShell: DynamicParam: get list of passed parameters?

下午好

不幸的是,PowerShell 无法通过参数类型检测 ParameterSet,例如:如果第二个参数作为 Int 传递,则 select ParameterSet1,否则使用 ParameterSet2。

因此我想手动检测传递的Parameter-Combinations。
是否可以在DynamicParam中获取传递的参数列表,像这样?:

Function Log {
    [CmdletBinding()]
    Param ()
    DynamicParam {
        # Is is possible to access the passed Parameters?,
        # something like that:
        If (Args[0].ParameterName -eq 'Message') { … }

        # Or like this:
        If (Args[0].Value -eq '…') { … }
    }
    …
}

非常感谢您的帮助和指导!
托马斯

第一个发现是错误的!: "I've found the magic, by using $PSBoundParameters we can access the passed parameters."

这是正确但非常令人失望的答案:
这非常烦人且令人难以置信,但看起来 PowerShell 并未传递有关动态传递参数的任何信息。

以下示例使用了此处定义的 New-DynamicParameter 函数: Can I make a parameter set depend on the value of another parameter?

Function Test-DynamicParam {
    [CmdletBinding()]
    Param (
        [string]$FixArg
    )
    DynamicParam {
        # The content of $PSBoundParameters is just 
        # able to show the Params declared in Param():
        # Key     Value
        # ---     -----
        # FixArg  Hello

        # Add the DynamicParameter str1:
        New-DynamicParameter -Name 'DynArg' -Type 'string' -HelpMessage 'DynArg help'

        # Here, the content of $PSBoundParameters has not been adjusted:
        # Key     Value
        # ---     -----
        # FixArg  Hello
    }
    Begin {
        # Finally - but too late to dynamically react! -
        # $PSBoundParameters knows all Parameters (as expected):
        # Key     Value
        # ---     -----
        # FixArg  Hello
        # DynArg  World
    }
    Process {
        …
    }
}

# Pass a fixed and dynamic parameter
Test-DynamicParam -FixArg 'Hello' -DynArg 'World'