Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeatable Parameters in Powershell Function (Preferably Linked Parameter Sets)

I am wondering if it is possible (and if so how) to create repeatable (and hopefully linked) parameters in a PowerShell function. This is how am looking for this to work:

function foo()
{
    [CmdletBinding()]
    Params(
        [Parameter(Mandatory=$true,ParameterSetName="Default")]
        [Parameter(Mandatory=$true,ParameterSetName="Set1")]
        [Parameter(Mandatory=$true,ParameterSetName="Set2")]
         [string]$SomeParam1,
        [Parameter(Mandatory=$true,ParameterSetName="Set1")]
        [Parameter(Mandatory=$true,ParameterSetName="Set2")]
        *some magic here, likely, to make this repeatable*
         [string]$SomeRepeatableParam,
        [Parameter(Mandatory=$true,ParameterSetName="Set1")]
         [string]$SomeLinkedParam1,
        [Parameter(Mandatory=$true,ParameterSetName="Set2")]
         [string]$SomeLinkedParam2
    )
    Begin
    {
        *some code here*
    }
    Process
    {
        foreach ($val in $SomeRepeateableParam)
        {
            *some code here using param and its linked param*
        }
    }
    End
    {
        *some code here*
    }
}

And then call this function like so:

foo -SomeParam "MyParam" -SomeRepeatableParam "MyProperty1" -SomeLinkedParam1 "Tall" -SomeRepeatableParam "MyProperty2" -SomeLinkedParam2 "Wide"

and so on, being able to use the repeatable parameter as many times in a single call as I feel like it.

Can this be done? And if so how?

Thanks for your time.

EDIT: For clarity, I don't mean an array parameter, but a repeatable parameter in which the linked parameter sets can be matched to each instance of the repeatable parameter.

like image 895
Leonastas Avatar asked Jun 07 '26 15:06

Leonastas


1 Answers

Since PowerShell supports arrays as parameter values, there is generally no need to repeat a parameter.

There is no syntactic way to enforce the pairing (linking) of parameter values the way you intend, with repeating instances of the same parameter name, because parameter names must be unique (and even they didn't have to be unique, that alone wouldn't enforce the desired pairing).

You can, however, use parallel array parameters, and enforce their symmetry inside the function, e.g.:

function foo
{
    [CmdletBinding()]
    Param(
         [string]   $SomeParam1,
         [string[]] $SomeRepeatableParam,
         [string[]] $SomeLinkedParam
    )
    if ($SomeRepeatableParam.Count -ne $SomeLinkedParam.Count) {
      Throw "Please specify paired values for -SomeRepeatableParam and -SomeLinkedParam"
    }
    for ($i = 0; $i -lt $SomeRepeatableParam.Count; ++$i) {
       $SomeRepeatableParam[$i] + ': ' + $SomeLinkedParam[$i]
    }
}

You would then call it as follows (note the , to separate the array elements):

foo -SomeParam1 "MyParam" `
    -SomeRepeatableParam "MyProperty1", "MyProperty2" `
    -SomeLinkedParam     "Tall",        "Wide"
like image 188
mklement0 Avatar answered Jun 10 '26 10:06

mklement0



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!