locked
Dynamic parameter is null? RRS feed

  • Question

  • Hi!

    I have issues accessing my dynamic parameters, so here I create a dynamic parameter called TheParameter with a ValidateSet of Value1 & Value2. The only way I can access the values are through PSBoundParameters. 

    Function Test-DynParams
    {
        [CmdletBinding()]
        Param (
    
        )
    
        DynamicParam {
            $RuntimeParamDic  = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
    
            # 
            $ParameterName = 'TheParameter'
            $itemset = "Value1","Value2"
    
            $ParamAttrib  = New-Object System.Management.Automation.ParameterAttribute
            $ParamAttrib.Mandatory  = $true
            $ParamAttrib.ParameterSetName  = '__AllParameterSets'
            $AttribColl = New-Object  System.Collections.ObjectModel.Collection[System.Attribute]
            $AttribColl.Add($ParamAttrib)
            $AttribColl.Add((New-Object  System.Management.Automation.ValidateSetAttribute($ItemSet)))
            $RuntimeParam  = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName,  [string], $AttribColl)
            $RuntimeParamDic.Add($ParameterName,  $RuntimeParam)
     
    
            # Return Parameters!
            return  $RuntimeParamDic
        }
        Begin{
            
        }
        process{
            "TheParameter=$TheParameter"
            "PSBoundParameters=$($PSBoundParameters.GetEnumerator() | ? key -eq TheParameter |select -ExpandProperty Value)"
            
        }
    }
    

    Test function:

    PS C:\WINDOWS\system32> Test-DynParams -TheParameter Value1
    TheParameter=
    PSBoundParameters=Value1
    Tested on PSVersion 4.0 & 5.0

    Can anybody explain why first row is empty? And can you reproduce the problem?

    Thanks!

    Tuesday, March 6, 2018 8:15 AM

Answers

  • By design.

    \_(ツ)_/

    • Marked as answer by Samus-Aran Tuesday, March 6, 2018 11:00 AM
    Tuesday, March 6, 2018 11:00 AM
  • Function Test-DynParams
    {
        [CmdletBinding()]
        Param (
    
        )
    
        DynamicParam {
            $RuntimeParamDic  = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
    
            # 
            $ParameterName = 'TheParameter'
            $itemset = "Value1","Value2"
    
            $ParamAttrib  = New-Object System.Management.Automation.ParameterAttribute
            $ParamAttrib.Mandatory  = $true
            $ParamAttrib.ParameterSetName  = '__AllParameterSets'
            $AttribColl = New-Object  System.Collections.ObjectModel.Collection[System.Attribute]
            $AttribColl.Add($ParamAttrib)
            $AttribColl.Add((New-Object  System.Management.Automation.ValidateSetAttribute($ItemSet)))
            $RuntimeParam  = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName,  [string], $AttribColl)
            $RuntimeParamDic.Add($ParameterName,  $RuntimeParam)
     
    
            # Return Parameters!
            return  $RuntimeParamDic
        }
        Begin{
            $TheParameter
        }
        process{
            Write-Host "TheParameter = $($PSBoundParameters.TheParameter)" -Fore green
            $PSBoundParameters
        }
    }
    Test-DynParams -TheParameter Value2
    
    
    


    \_(ツ)_/

    • Marked as answer by Samus-Aran Tuesday, March 6, 2018 11:00 AM
    Tuesday, March 6, 2018 9:31 AM

All replies

  • Function Test-DynParams
    {
        [CmdletBinding()]
        Param (
    
        )
    
        DynamicParam {
            $RuntimeParamDic  = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
    
            # 
            $ParameterName = 'TheParameter'
            $itemset = "Value1","Value2"
    
            $ParamAttrib  = New-Object System.Management.Automation.ParameterAttribute
            $ParamAttrib.Mandatory  = $true
            $ParamAttrib.ParameterSetName  = '__AllParameterSets'
            $AttribColl = New-Object  System.Collections.ObjectModel.Collection[System.Attribute]
            $AttribColl.Add($ParamAttrib)
            $AttribColl.Add((New-Object  System.Management.Automation.ValidateSetAttribute($ItemSet)))
            $RuntimeParam  = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName,  [string], $AttribColl)
            $RuntimeParamDic.Add($ParameterName,  $RuntimeParam)
     
    
            # Return Parameters!
            return  $RuntimeParamDic
        }
        Begin{
            $TheParameter
        }
        process{
            Write-Host "TheParameter = $($PSBoundParameters.TheParameter)" -Fore green
            $PSBoundParameters
        }
    }
    Test-DynParams -TheParameter Value2
    
    
    


    \_(ツ)_/

    • Marked as answer by Samus-Aran Tuesday, March 6, 2018 11:00 AM
    Tuesday, March 6, 2018 9:31 AM
  • Ah thanks jrv, that's shorter!

    Is it by design that you should get the value from $PSboundParameters instead of just call it with $TheParameter?

    Tuesday, March 6, 2018 10:59 AM
  • By design.

    \_(ツ)_/

    • Marked as answer by Samus-Aran Tuesday, March 6, 2018 11:00 AM
    Tuesday, March 6, 2018 11:00 AM
  • Ah ok, thanks!
    Tuesday, March 6, 2018 11:00 AM