Parameter binding problem with ValueFromRemainingArguments in PS functions
See original GitHub issueSteps to reproduce
Define a PowerShell function with an array parameter using the ValueFromRemainingArguments property of the Parameter attribute. Instead of sending multiple arguments, send that parameter a single array argument.
& {
param(
[string]
[Parameter(Position=0)]
$Root,
[string[]]
[Parameter(Position=1, ValueFromRemainingArguments)]
$Extra)
$Extra.Count;
for ($i = 0; $i -lt $Extra.Count; $i++)
{
"${i}: $($Extra[$i])"
}
} root aa,bb
Expected behavior
The array should be bound to the parameter just as you sent it, the same way it works for cmdlets. (The “ValueFromRemainingArguments” behavior isn’t used, in this case, it should just bind like any other array parameter type.) The output of the above script block should be:
2 0: aa 1: bb
Actual behavior
PowerShell appears to be performing type conversion on the argument to treat the array as a single element of the parameter’s array, instead of checking first to see if more arguments will be bound as “remaining arguments” first. The output of the above script block is currently:
1 0: aa bb
Additional information
To demonstrate that the behavior of cmdlets is different, you can use this code:
Add-Type -OutputAssembly $env:temp\testBinding.dll -TypeDefinition @'
using System;
using System.Management.Automation;
[Cmdlet("Test", "Binding")]
public class TestBindingCommand : PSCmdlet
{
[Parameter(Position = 0)]
public string Root { get; set; }
[Parameter(Position = 1, ValueFromRemainingArguments = true)]
public string[] Extra { get; set; }
protected override void ProcessRecord()
{
WriteObject(Extra.Length);
for (int i = 0; i < Extra.Length; i++)
{
WriteObject(String.Format("{0}: {1}", i, Extra[i]));
}
}
}
'@
Import-Module $env:temp\testBinding.dll
Test-Binding root aa,bb
Environment data
> $PSVersionTable
Name Value
---- -----
PSEdition Core
PSVersion 6.0.0-alpha
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
WSManStackVersion 3.0
GitCommitId v6.0.0-alpha.9-107-g203ace04c09dbbc1ac00d6b497849cb69cc919fb-dirty
PSRemotingProtocolVersion 2.3
CLRVersion
SerializationVersion 1.1.0.1
BuildVersion 3.0.0.0
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:29 (22 by maintainers)
What a non-event that was. No merge conflicts. 😃
I agree with @rkeithhill the current parameter binder for ValueFromRemainingArguments is working as expected - its by design and no fix needed.