question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Parameter binding problem with ValueFromRemainingArguments in PS functions

See original GitHub issue

Steps 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:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:29 (22 by maintainers)

github_iconTop GitHub Comments

1reaction
dlwyattcommented, May 25, 2017

What a non-event that was. No merge conflicts. 😃

1reaction
iSazonovcommented, Sep 26, 2016

I agree with @rkeithhill the current parameter binder for ValueFromRemainingArguments is working as expected - its by design and no fix needed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Developers - Parameter binding problem with ...
Define a PowerShell function with an array parameter using the ValueFromRemainingArguments property of the Parameter attribute. Instead of ...
Read more >
Powershell Parameter Set and
1 Answer 1 · You function is invalid. Missing function keyword · There's no need for ParameterSets to have one optional parameter. ·...
Read more >
about Functions Advanced Parameters - PowerShell
Explains how to add parameters to advanced functions. ... conversion is performed during parameter binding for compiled cmdlets.
Read more >
Splatting and mandatory parameters
The ValueFromRemainingArguments argument indicates that the parameter accepts all of the parameters values in the command that are not assigned ...
Read more >
Getting an ambiguous parameter error even though I'm ...
I would assume using ValueFromRemainingArguments would take ... It seems that cmdletbinding() is automatically included when I use parameter ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found