如何从powershell中获取DynamicParam的管道值

时间:2015-02-19 10:36:16

标签: powershell validateset

我的目标是为支持两者的powershell函数提供参数

  1. 仅在运行时
  2. 中已知的集合的ValidateSet(以及tab-complition)
  3. 通过管道提供参数的能力。
  4. 我能够达到#1,但看起来#2失败了。

    以下是我的代码的简化示例: 最初我有一个简单的函数,打印提供给函数的所有参数名称。 ValidateSet是静态的,不会在运行时生成。该函数定义如下:

    Function Test-Static {
        [CmdletBinding()]
        Param(
            [Parameter(Mandatory=$true, ValueFromPipeline = $true, Position=1)]
            [ValidateSet("val1","val2")]
            $Static
        )
    
        begin {}
        process {
        Write-Host "bound parameters: $($PSBoundParameters.Keys)"
        }
    }
    

    运行以下代码时

    "val1" | Test-Static

    输出

    bound parameters: Static

    然后我继续尝试使用动态参数执行完全相同的操作,但看起来$PsBoundParameters为空。请注意,如果我将值作为参数而不是通过管道提供,那么 会显示在$PsBoundParameters中。

    Function Test-Dynamic {
        [CmdletBinding()]
        Param(
        )
    
        DynamicParam {
                # Set the dynamic parameters' name
                $ParameterName = 'Dynamic'
    
                # Create the dictionary 
                $RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
    
                # Create the collection of attributes
                $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
    
                # Create and set the parameters' attributes
                $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute
                $ParameterAttribute.Mandatory = $true
                $ParameterAttribute.Position = 1
                $ParameterAttribute.ValueFromPipeline = $true
    
                # Add the attributes to the attributes collection
                $AttributeCollection.Add($ParameterAttribute)
    
                # Generate and set the ValidateSet 
                $arrSet = "val1","val2"
                $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet)
    
                # Add the ValidateSet to the attributes collection
                $AttributeCollection.Add($ValidateSetAttribute)
    
                # Create and return the dynamic parameter
                $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName, [string], $AttributeCollection)
                $RuntimeParameterDictionary.Add($ParameterName, $RuntimeParameter)
                return $RuntimeParameterDictionary
        }
    
        begin {
            # Bind the parameter to a friendly variable
            write-host "bound parameters: $($PsBoundParameters.Keys)"
            $Param = $PsBoundParameters[$ParameterName]
        }
    
        process {
        }
    
    }
    

    运行时

    "val1" | test-Dynamic我得到以下结果:

    bound parameters:

    基本上意味着没有参数绑定。

    我做错了什么?我怎样才能实现我的最初目标?

1 个答案:

答案 0 :(得分:2)

@CB在这里有正确的想法。

您无法从begin块访问管道数据;仅来自process块。

begin块如果作为命名或位置参数传递,则可以访问该参数,但不能通过管道传递。

无论您是否使用动态参数,都是如此。