Skip to content

Using a dynamicparam block in an advanced function causes invocations with the -PipelineVariable common parameter to fail #6420

Description

An (adapted) copy of this uservoice issue first reported by PetSerAl in 2014:

Using a dynamicparam block in an advanced function causes invocations with the -PipelineVariable common parameter to fail.

Steps to reproduce

function foo {
  [CmdletBinding()]
  param()
  dynamicparam { }
  process { 'hi' }
}

foo -PipelineVariable bar

Expected behavior

hi

Actual behavior

The following error is reported:

foo : Cannot retrieve the dynamic parameters for the cmdlet. Object reference not set to an instance of an object.
...

Note that it doesn't make a difference whether the dynamicparam block is empty or not.

Environment data

PowerShell Core v6.0.2 on macOS 10.13.3
PowerShell Core v6.0.2 on Ubuntu 16.04.3 LTS
PowerShell Core v6.0.2 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)
Windows PowerShell v5.1.15063.674 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)

Activity

  1. MaximoTrinidad commented on Mar 19, 2018

    @MaximoTrinidad

    Just an FYI

    All example I'm found on the internet involves more code in the DynamicParam scriptblock area.

    I can't find no simple sample like the one Michael Klement (@mklement0) has provided.

    There's always some complex coding in it:
    https://stackoverflow.com/questions/42318419/powershell-using-dynamic-parameters-value
    http://www.powershellmagazine.com/2014/05/29/dynamic-parameters-in-powershell/

    The simplest code I found: https://community.spiceworks.com/topic/1694525-powershell-dynamic-parameters

    I wonder if it's an overkill. Of course, I haven't use it.
    :)

  2. mklement0 commented on Mar 19, 2018

    @mklement0
    ContributorAuthor

    Maximo Trinidad (@MaximoTrinidad):

    Here's a relatively simple working example that defines a dynamic -baz parameter that uses a validate-set attribute to only accept value 1, 2, or 3.
    As you'll see, the problem with -PipelineVariable persists; try foo -pv var.

    function foo {
        [CmdletBinding()] # !! Required for DynamicParam {} to work properly.
        param([string] $bar)
        DynamicParam
        {
            # Create the dynamic parameter's attributes.
            # -- Create a [Parameter()] attribute.
            $paramAttr = [System.Management.Automation.ParameterAttribute]::new()
            # Set properties such as .ParameterSetName, .Position, .ValueFromPipeline, ...
            $paramAttr.Position = 1
            # -- Create a [ValidateSet()] attribute with a set of permissive values.
            $validateSetAttr = [System.Management.Automation.ValidateSetAttribute]::new(1, 2, 3)
    
            # Create the dynamic parameter with its name, type, and attributes...
            $dynParam1 = [System.Management.Automation.RuntimeDefinedParameter]::new('baz', [int],
                ($paramAttr, $validateSetAttr)
            )
    
            # ... and add it to a RuntimeDefinedParameterDictionary to return.
            $paramDict = [System.Management.Automation.RuntimeDefinedParameterDictionary]::new()
            $paramDict.Add($dynParam1.Name, $dynParam1)
            return $paramDict
        }
        end {
            $bar
            $baz
        }
    }
  3. MaximoTrinidad commented on Mar 19, 2018

    @MaximoTrinidad
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions