You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Using a dynamicparam block in an advanced function causes invocations with the -PipelineVariable common parameter to fail #6420
Using a dynamicparam block in an advanced function causes invocations with the -PipelineVariable common parameter to fail.
Steps to reproduce
functionfoo {
[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)
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.
functionfoo {
[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
}
}
Activity
MaximoTrinidad commentedon Mar 19, 2018
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.
:)
mklement0 commentedon Mar 19, 2018
Maximo Trinidad (@MaximoTrinidad):
Here's a relatively simple working example that defines a dynamic
-bazparameter that uses a validate-set attribute to only accept value1,2, or3.As you'll see, the problem with
-PipelineVariablepersists; tryfoo -pv var.MaximoTrinidad commentedon Mar 19, 2018
Thanks Michael Klement (@mklement0)!
:)