How can I pass parameters to powershell functions?

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 for Test-Multi and pass them directly to the underlying perl script legacy.

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

+6
source share
2 answers

After viewing the comment, parameter 3 sounds exactly the way you want.


You have several options:

  • Use $args (credit hjpotter92 answer )

  • Define your additional parameters clearly, then analyze them all in your function to add them to your perl call.

  • Use one parameter with argument ValueFromRemainingArguments , 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 } 
+6
source

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

+1
source

All Articles