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?
To pass multiple parameters you must use the command line syntax that includes the names of the parameters. For example, here is a sample PowerShell script that runs the Get-Service function with two parameters. The parameters are the name of the service(s) and the name of the Computer.
One way to use PowerShell function parameters in a script is via parameter name -- this method is called named parameters. When calling a script or function via named parameters, use the entire name of the parameter. For example, perhaps the example param block above is stored in a PowerShell script called foo. ps1.
The * symbol is used to pass a variable number of arguments to a function. Typically, this syntax is used to avoid the code failing when we don't know how many arguments will be sent to the function.
After seeing your comment, option 3 sounds like exactly what you want.
You have a few options:
Use $args
(credit to hjpotter92's answer)
Explicitly define your additional parameters, then parse them all in your function to add them to your perl call.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With