Skip to content

Passing a ScriptBlock (in syntax only) to native commands produces surprising results #5187

Closed

Description

@lzybkr

Steps to reproduce

Run a command like

echoargs {1}
bcdedit /set {cbd971bf-b7b8-4885-951a-fa03044f5d71} device partition=C:

PowerShell has long assumed that an argument that looks like a ScriptBlock is always a script block - regardless of what command is being called. If the command is native, a very different command line is passed.

This doesn't make sense for most native commands. Long ago there was a vision that there might be many native commands that supported PowerShell and maybe this made sense then, but that hasn't happened.

The workaround is to use quotes - not a bad workaround but still shouldn't be necessary.

If we changed the behavior, we would still pass these special parameters to powershell.exe and pwsh.exe.

Expected behavior

PS> echoargs {1}
arg 0: <{1}>

CommandLine:
EchoArgs.exe {1}

Actual behavior

PS> echoargs {1}
arg 0: <-encodedCommand>
arg 1: <MQA=>
arg 2: <-inputFormat>
arg 3: <xml>
arg 4: <-outputFormat>
arg 5: <text>

CommandLine:
EchoArgs.exe -encodedCommand MQA= -inputFormat xml -outputFormat text

Activity

  1. added
    Breaking-Change
    Issue-Discussion
    on Oct 21, 2017
  2. SteveL-MSFT commented on Oct 21, 2017

    @SteveL-MSFT
    Member

    So the expected behavior is that if the command is native, braces are literal always? What about parenthesis?

  3. lzybkr commented on Oct 21, 2017

    @lzybkr
    ContributorAuthor

    I'm suggesting syntax doesn't matter - if the value is a ScriptBlock, pass the literal text unless calling powershell.exe or pwsh.exe, then use -encodedCommand.

    We could attempt to be smart too - only pass the literal if the ScriptBlock has no whitespace, or I suppose escaped whitespace

  4. SteveL-MSFT commented on Oct 25, 2017

    @SteveL-MSFT
    Member

    Isn't this a dupe of #1761?

  5. lzybkr commented on Oct 26, 2017

    @lzybkr
    ContributorAuthor

    Not really a dupe.

  6. BrucePay commented on Mar 9, 2018

    @BrucePay
    Collaborator

    Jason Shirk (@lzybkr) Historical context: this feature was introduced when we were still having to consider mini-shells. In that scenario, we were expecting many, many shells - essentially one mini-shell executable per module - all with unknown names. And so we autoconverted scriptblocks for all executables. Obviously that is no longer the case however people may have aliases/symlinks for our binaries (similar to python & python3). So there is still a chance that there will be unknown binaries. As an alternative to hard coding the list of executables, we could tweak the converter to special-case GUIDs. Are there other common cases where we'd want scriptblocks passed to executable verbatim?

  7. lzybkr commented on Mar 9, 2018

    @lzybkr
    ContributorAuthor

    Bruce Payette (@BrucePay) - bcdedit accepts names in place of a GUID, e.g.: bcdedit /default {ntldr}.

    I suppose a heuristic is possible though - if there are no spaces, it's unlikely to be a script block. Furthermore, if the text specifies calling a single command with no arguments and that command doesn't exist, that is very strong evidence that it's not really a script block - that would cover most use cases.

    That said, I've never seen another executable that relies on this behavior. Introducing quirky behavior on the off chance that a non-PowerShell binary expects the current behavior is maybe being a little too paranoid w.r.t. compatibility.

  8. mklement0 commented on Oct 9, 2018

    @mklement0
    Contributor

    As for the symlink issue (the PowerShell executable in disguise): resolving symlinks to their ultimate target to check the latter's name sounds like a simple solution.

    I agree that not employing heuristics is best - both in this case and #1761 - because in the long run users are better served by simply having to remember - and being able to rely on - one simple rule:

    PowerShell's own syntax requirements must be satisfied first, which means that any metacharacters recognized by PowerShell must be quoted for literal use in an argument.

    Yes, for users unfamiliar with PowerShell's syntax that involves growing pains - given that more characters are metacharacters than in other shells, especially compared to cmd.exe, but the principle is the same: Just like you must double-quote a literal & in cmd.exe, for instance, you must quote a {...} token meant to be a literal.

    In the long run, and across different scenarios, not having to remember heuristics and exceptions is a blessing.

    The workaround is to use quotes - not a bad workaround but still shouldn't be necessary.

    Therefore, I wouldn't frame the need to use quoting as a workaround - it's simply a syntactic necessity in PowerShell.

    As PowerShell sees wider adoption - and perhaps even becomes the single, cross-platform choice for users - these issues will lessen:

    • users will know the syntax requirements
    • more command lines that use PowerShell syntax to begin with will appear online

    That said (a separate issue), the broken treatment of double quotes will continue to be a pain point.


    As for helping users understand:

    In the case at hand, applying the usual rules - stringification of a script block to its literal content - will at least tell users that the { and } were "eaten" during invocation, which is a starting point for investigation.

    In #1761 the symptom is more obscure.

    Providing a way to inspect the actual arguments passed would definitely be helpful, as originally discussed in #1761 (comment).

  9. added this to the Future milestone on Oct 9, 2019
  10. yecril71pl commented on Aug 21, 2020

    @yecril71pl
    Contributor

    Could we just limit the encoding to script blocks following -Command or -c? (Unfortunately, -c is rather common, but we must support it anyway.)

  11. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  12. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  13. added
    Resolution-No Activity
    on Nov 16, 2023
  14. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  15. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Breaking-ChangeIssue-DiscussionResolution-No Activity

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions