29

I have a function that looks something like this:

function global:Test-Multi {
    Param([string]$Suite)
    & perl -S "$Suite\runall.pl" -procs:$env:NUMBER_OF_PROCESSORS
}

I would like to allow the user to specify more parameters to Test-Multi and pass them directly to the underlying legacy perl script.

Does powershell provide a mechanism to allow additional variadic behavior for this purpose?

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
Billy ONeal
  • 104,103
  • 58
  • 317
  • 552

3 Answers3

28

After seeing your comment, option 3 sounds like exactly what you want.


You have a few options:

  1. Use $args (credit to hjpotter92's answer)

  2. Explicitly define your additional parameters, then parse them all in your function to add them to your perl call.

  3. Use a single parameter with the ValueFromRemainingArguments argument, e.g.

     function global:Test-Multi {
         Param(
             [string]$Suite,
             [parameter(ValueFromRemainingArguments = $true)]
             [string[]]$Passthrough
             )
         & perl -S "$Suite\runall.pl" -procs:$env:NUMBER_OF_PROCESSORS @Passthrough
     }
    
RobH
  • 3,604
  • 1
  • 23
  • 46
briantist
  • 45,546
  • 6
  • 82
  • 127
12

$args is not going to pass through arguments correctly. if you want the arguments to remain as separate arguments, you should use @args instead.

tiennen07
  • 366
  • 3
  • 6
4

I'm not sure about what you wish to achieve, but the arguments passed to a function are accessible in the $args variable available inside the function.

hjpotter92
  • 78,589
  • 36
  • 144
  • 183