Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the simplest form of Dynamic Parameters?

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
like image 486
Abraham Zinala Avatar asked Dec 30 '21 03:12

Abraham Zinala


People also ask

What is the dynamic parameter?

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.

What are dynamic parameters in tableau?

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.

What is dynamic parameter sap?

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.

What is dynamic parameter in PowerShell?

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.


2 Answers

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

  • autocomplete module names for Get-Command -Module
  • or search for files by filetype, only complete filetypes if they exist in that directory

Windows Powershell

You can use DynamicValidateSet pattern in WindowsPowershell

gif using DynamicValidateSet

https://i.stack.imgur.com/nnLBS.gif

New in PowerShell

6 adds Suggestions

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,

7.2 Generic Completers

New [ArgumentCompleter] attribute with the [IArgumentCompleterFactory] interface

from the docs:

[DirectoryCompleter(ContainingFile="pswh.exe", Depth=2)]

[DateCompleter(WeekDay='Monday', From="LastYear")]

[GitCommits(Branch='release')]
like image 86
ninMonkey Avatar answered Oct 24 '22 12:10

ninMonkey


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
like image 32
BETOMBO Avatar answered Oct 24 '22 10:10

BETOMBO