简体   繁体   English

Powershell函数中的可重复参数(最好是链接参数集)

[英]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. 我想知道是否有可能(如果是这样的话)在PowerShell函数中创建可重复(并且希望链接)的参数。 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. 编辑:为清楚起见,我不是指一个数组参数,而是一个可重复的参数,其中链接的参数集可以与可重复参数的每个实例匹配。

Since PowerShell supports arrays as parameter values, there is generally no need to repeat a parameter. 由于PowerShell支持将数组作为参数值,因此通常无需重复参数。

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 , eg: 但是,您可以使用并行数组参数,并在函数内强制实现它们的对称性,例如:

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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM