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 parsing/passing: in native executable calls, arguments that look like named PowerShell arguments do not treat the grouping operator as a separate argument

See original GitHub issue

Follow-up from https://github.com/PowerShell/PowerShell/issues/1995#issuecomment-676617510. Related: #13489, #6360, #6291

In short: Using an expression (via (...), the grouping operator) as part of an argument normally causes the output from that expression to be passed as a separate argument (which in itself may be surprising - see #6467).

This is not the case in the following, very specific scenario:

  • An external (native) executable is being called.

  • The start of the argument looks something like -foo:, i.e. looks like a named PowerShell argument, though that concept doesn’t apply when calling external programs; note that any other prefix before the (...) does not trigger the symptom.

In that event, the (stringified) (...) output is directly attached to -foo: to form a single argument, though note that if the output is a collection, only the first element is attached.

There is no good reason for this anomaly, and the behavior with collections is virtually useless.

Steps to reproduce

# OK: PowerShell command: the output from (...) is passed as a *separate* argument.
Write-Output -foo:('bar', 'baz') | Should -BeExactly '-foo:', ('bar', 'baz')

# On Unix:
# BROKEN: External program: the output from (...) is partially passed as part of `-foo:`, namely 
# (only) the *first* array element.
printf '%s\n' -foo:('bar', 'baz') | Should -Be '-foo:', 'bar', 'baz'

Expected behavior

The tests should pass.

Actual behavior

The second test fails:

Expected @('-foo:', 'bar', 'baz'), but got @('-foo:bar', 'baz')

Note how only 2 arguments were passed, because the first array element, bar, was directly attached to -foo:.

Environment data

PowerShell Core 7.1.0-preview.6

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:10 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
vexx32commented, Aug 20, 2020

/cc @JamesWTruher for visibility.

@mklement0 I half suspect you must spend a lot of time trying to break things around this 😂 (But honestly, thanks; I doubt we’d find half these things if you didn’t!)

0reactions
mklement0commented, Aug 21, 2020

Actually, the call PWSH A.PS1 -A:1,2 works as well.

No, that passes'1,2' as single string (i.e, neither string '1 2' nor array 1, 2).

I deny any wrongdoing with the syntax -A:$A

This is another manifestation of #13489 - it should be treated the same as "-A:$A" and therefore pass a single argument with verbatim content -A:1 2, but it currently behaves the same (broken) way as -A:(1,2).

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to launch an executable with arguments integrated in ...
I am trying to launch an executable from a PowerShell script. ... passed as the "-Path" argument without a separate "-Arguments" parameter?
Read more >
PowerShell's parsing modes: argument (command) ...
PowerShell has two fundamental parsing modes: argument mode, which works like traditional shells. In argument mode, the first token is ...
Read more >
Native Commands in PowerShell - A New Approach
In this two part blog post I'm going to investigate how PowerShell can take better advantage of native executables. In the first post, ......
Read more >
Native Commands in PowerShell - A New Approach - Part 2
In this post, I'll be going through a couple of experiments I've done with the kubernetes kubectl utility. Is there a better way....
Read more >
powershell - ParameterArgumentTransformationError
I have a script, see below, that searches for the most recent MSDeploy executable on the sys drive. However, my Compare-FileVersion function is...
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