Trying to get a better understanding of Dynamic Parameters, but something keeps bothering me. All examples I've found across the outer-webs (internet), the parameter attributes are always defined/included.
Here's what I'm working with:
Function Test-DynamicParameters {
[cmdletbinding()]
Param (
[System.IO.DirectoryInfo]$Path
)
DynamicParam
{
if ($Path.Extension) {
$parameterAttribute = [System.Management.Automation.ParameterAttribute]@{
Mandatory = $false
}
$attributeCollection = [System.Collections.ObjectModel.Collection[System.Attribute]]::new()
$attributeCollection.Add($parameterAttribute)
$dynParam1 = [System.Management.Automation.RuntimeDefinedParameter]::new(
'TestSwitch', [switch], $attributeCollection
)
$paramDictionary = [System.Management.Automation.RuntimeDefinedParameterDictionary]::new()
$paramDictionary.Add('TestSwitch', $dynParam1)
$paramDictionary
}
}
Begin { }
Process
{
$PSBoundParameters
}
End { }
}
. . .currently have tried numerous combinations of removing certain lines/code to see what would make my Dynamic Parameter show, but nothing works without the attributes being declared. Is this necessary?
Question: what is the simplest form of Dynamic Parameter declaration that I would need for it to work?
For Example - can it just be shortened to where I only define just the name? Or, would PowerShell insist a type is specified instead of defaulting to [object]
? Something like:
$paramDictionary = [System.Management.Automation.RuntimeDefinedParameterDictionary]::new()
$paramDictionary.Add('TestSwitch')
$paramDictionary
Dynamic parameters can be strings, measure values, or dates, offering flexibility and interactivity when building a dashboard for your audience. Because they can be easily used in most analytical entities, dynamic parameters give you programmatic control to customize your analysis.
In version 2020.1, Tableau introduced a much-anticipated feature, dynamic parameters, which allows us to automatically populate a parameter's allowed values based on data as well as updating the current value based on some condition.
When a profile parameter is changed dynamically, the current value of the profile parameter on the instance or across the system is replaced with the new value. This dynamically changed value is valid for as long as the application server instance is running.
These parameters are added at runtime and are referred to as dynamic parameters because they're only added when needed. For example, you can design a cmdlet that adds several parameters only when a specific switch parameter is specified. Note. Providers and PowerShell functions can also define dynamic parameters.
What kind of behavior are you looking for? Because dynamic parameters are usually not needed, even if the auto-completion is dynamic.
For example you could do
Get-Command -Module
You can use DynamicValidateSet pattern in WindowsPowershell
gif using DynamicValidateSet
New: [IValidateSetValuesGenerator]
and [ArgumentCompletions]
It autocompletes like [ValidateSet]
, but the user is still free to enter others.
Note, [ArgumentCompletions]
and [ArgumentCompleter]
are two different classes.
[Parameter(Mandatory)]
[ArgumentCompletions('Fruits', 'Vegetables')]
[string]$Type,
New [ArgumentCompleter] attribute with the [IArgumentCompleterFactory]
interface
from the docs:
[DirectoryCompleter(ContainingFile="pswh.exe", Depth=2)]
[DateCompleter(WeekDay='Monday', From="LastYear")]
[GitCommits(Branch='release')]
You can’t as the function add of the type System.Management.Automation.RuntimeDefinedParameterDictionary require to provide a key and value which is exactly a String (key) and a
System::Management::Automation::RuntimeDefinedParameter
for the value. For more information you can check the class documentation here https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.runtimedefinedparameterdictionary?view=powershellsdk-7.0.0.
I have tested on my side and you need at least the ParameterAttribute
Mandatory
set to true in order to make it run, and the key/name used in the RuntimeDefinedParameter
must match the key used in the dictionary when you add it.
So the minimum code that you need should be like this:
Function Test-DynamicParameters {
[cmdletbinding()]
Param (
[System.IO.FileSystemInfo ]$Path
)
DynamicParam
{
if ($Path.Extension) {
$parameterAttribute = [System.Management.Automation.ParameterAttribute]@{
Mandatory = $true
}
$attributeCollection = [System.Collections.ObjectModel.Collection[System.Attribute]]::new()
$attributeCollection.Add($parameterAttribute)
$dynParam1 = [System.Management.Automation.RuntimeDefinedParameter]::new("TestSwitch", [string], $attributeCollection)
$paramDictionary = [System.Management.Automation.RuntimeDefinedParameterDictionary]::new()
$paramDictionary.Add('TestSwitch', $dynParam1)
$paramDictionary
}
}
Begin { }
Process
{
$PSBoundParameters
}
End { }
}
$FileNamePath = "C:\tmp\stackoverflow\test.txt";
$path = (Get-Item $FileNamePath )
Test-DynamicParameters -Path $path
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With