Skip to content

Passing a script block to an external utility should only trigger Base64 encoding if the target is powershell -command #4973

Closed

Description

Related: #4966

Currently, any script block you pass to an external utility from within PowerShell translates translation of the script block into -encodedCommand <base64-encoded-script-block-string> -inputFormat xml -outputFormat text (you can override the -inputFormat and -outputFormat values by passing them explicitly, however), even though that only makes sense when invoking (another instance of) PowerShell.

PetSerAl has noted that it may be non-trivial to robustly detect when PowerShell is being called and that passing a script block to something other than PowerShell is generally not useful, but the current behavior is surprising and should at least be documented.

An easy improvement would be to restrict the implicit Base64-encoding to commands where -Command (or an unambiguous prefix) precedes the script block.

As for what should happen when it is not PowerShell that is targeted: arguably, a script block should be stringified and the result passed as an argument (e.g., { "hi" } -> "hi").

Steps to reproduce

Run from PowerShell on a Unix platform:

powershell -noprofile -command { "hi" }
/bin/echo { "hi" }

Expected behavior

hi
 "hi"   # surrounded by a space on either side - the result of { "hi" }.ToString()

Actual behavior

hi
-encodedCommand IAAiAGgAaQAiACAA -inputFormat xml -outputFormat text

Environment data

PowerShell Core v6.0.0-beta.7 on macOS 10.12.6
PowerShell Core v6.0.0-beta.7 on Ubuntu 16.04.3 LTS
PowerShell Core v6.0.0-beta.7 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)
Windows PowerShell v5.1.15063.502 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)

Activity

  1. added
    Issue-Discussion
    on Oct 2, 2017
  2. PetSerAl commented on Oct 2, 2017

    @PetSerAl
    Contributor

    Nitpicking, but it not always -inputFormat xml -outputFormat text:

    cmd /c echo { } -I T | echo
  3. mklement0 commented on Oct 2, 2017

    @mklement0
    ContributorAuthor

    Thanks, PetSerAl - I've updated the original post to clarify that you can override -inputFormat and -outputFormat.

  4. KalleOlaviNiemitalo commented on Sep 29, 2023

    @KalleOlaviNiemitalo

    I'd prefer having this just throw an error "Cannot pass a script block to a native application", at least in strict mode. The current behaviour looks rather strange if I forget to use quotation marks or --%:

    PS C:\[REDACTED]\master> git rev-parse 1.15.4^{commit}
    fatal: ambiguous argument 'YwBvAG0AbQBpAHQA': unknown revision or path not in the working tree.
    Use '--' to separate paths from revisions, like this:
    'git <command> [<revision>...] -- [<file>...]'
    459878e67afb7ff3171072f4004037dbbc65aff3
    -encodedCommand
    YwBvAG0AbQBpAHQA
    
  5. added
    Resolution-No Activity
    on Mar 27, 2024
  6. microsoft-github-policy-service commented on Mar 27, 2024

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if there is no further activity in 7 days, the issue will be closed automatically.

    Activity in this case refers only to comments on the issue. If the issue is closed and you are the author, you can re-open the issue using the button below. Please add more information to be considered during retriage. If you are not the author but the issue is impacting you after it has been closed, please submit a new issue with updated details and a link to this issue and the original.

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

    Issue-DiscussionResolution-No Activity

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions