Repository navigation
Description
Note that this applies to calling external programs, including PowerShell's CLI, from within PowerShell
Passing a single script block containing the entire command to the PowerShell executable is a convenient way to pass a command from within PowerShell (a) without having to worry about quoting and (b) actually getting deserialized objects back (not just text).
PS> pwsh -noprofile { 'hi' } # -Command implied.
hi # OKBehind the scenes, the script block is Base64-encoded and the command line is transformed to using the -EncodedCommand parameter with the encoded script block (along with parameters -inputFormat xml and -outputFormat xml)
However, this transformation is mistakenly also applied if you attempt to pass a script block as an individual argument to a -File command:
Update: More generally, the inappropriate argument transformation is applied when invoking any external program, and a script block is used as any of its arguments; e.g., /bin/echo { 'hi' }
Steps to reproduce
Describe "Pass a script block as an argument with -File" {
BeforeAll {
Push-Location testdrive:\
# Test script that simply echoes its arguments.
'$args' > test.ps1
}
It "Passes the script block as a string." {
# The script block should be stringified and therefore be passed
# as ' hi '
pwsh -noprofile -file ./test.ps1 { 'hi' } | Should -Be ' hi '
}
AfterAll {
Pop-Location
}
}Expected behavior
The test should pass.
Actual behavior
The test fails:
Expected ' hi ', but got
@('-encodedCommand', 'IAAnAGgAaQAnACAA', '-inputFormat', 'xml', '-outputFormat', 'xml').
Note how the script-block argument - meant to be an argument to the script passed to -File - was inappropriately translated into the -encodedCommand ... arguments and passed to the target script.
Environment data
PowerShell Core 7.0.0-preview.4
Activity
[-]CLI: script block as individual arguments break the command[/-][+]CLI: script blocks as -File arguments break the command[/+]BrucePay commentedon Oct 21, 2019
Filemakes no sense as the parameter requires a file name.mklement0 commentedon Oct 21, 2019
Thanks, Bruce Payette (@BrucePay) - how does any of this (especially 2.) relate to this bug report, which is about an obvious mis-application of the base64-encoding-behind-the-scenes technique?
To recap from the OP:
Once we agree that this is a bug (which seems pretty obvious to me), we can talk about what the correct behavior should be in this - exotic - scenario:
Given:
i.e., simple
.ToString()stringification is normally applied, so the same should apply to script blocks:That is, the literal contents of the script block should be passed - that this may break due to PowerShell's broken quoting for external programs is a separate issue.
Just to demonstrate that the current behavior doesn't make sense (actually a simpler repro than in the OP):
Oops.
BrucePay commentedon Oct 22, 2019
That is explicitly not true for scriptblocks. The current behaviour is entirely by design. Whenever a scriptblock is passed, it is always passed encoded so the following can work:
mklement0 commentedon Oct 22, 2019
Bruce Payette (@BrucePay):
That makes perfect sense when calling the PowerShell CLI - and there only if the encoded command makes up everything to execute.
It makes no sense when calling other external programs - and that is the bug.
To recap (on macOS and Linux):
Passing these arguments to the
/bin/echoUnix utility is self-evidently pointless.BrucePay commentedon Oct 24, 2019
Since PowerShell is hostable, there is no way to know all of the executables that could take script block so we do it for everything. Conversely, there is no reason to try to pass scriptblocks to anything but a powershell host so why does it matter?
Even if it wasn't encoded it would be pointless so why would you do it?
mklement0 commentedon Oct 25, 2019
Bruce Payette (@BrucePay):
Let's start with an external program that we know to host PowerShell:
It clearly doesn't make sense to expect arbitrary script file
test.ps1to know how to to interpret these arguments, which are solely meaningful to PowerShell's own CLI.Even if we - unwarrantedly - assume that all external programs host PowerShell, there is no no reason to assume that they speak this particular flavor of CLI syntax - that is not a requirement for programs hosting PowerShell.
In short: Translating script blocks to
-encodedCommand ... -inputFormat xml -outputFormat textonly ever makes sense:pwsh[.exe]orpowershell.exeis the external program being invoked-c/-Commandargument.That this was the - incompletely implemented - original intent can be gleaned from the error message emitted by the following:
Any other application of this argument translation is pointless and a bug worth fixing - see OP.
[-]CLI: script blocks as -File arguments break the command[/-][+]CLI: script blocks as external program arguments break the command[/+]mklement0 commentedon Oct 28, 2019
As for when people may want to pass script blocks to non-PowerShell commands:
Script blocks are often - treacherously - repurposed to serve as string literals that make embedded quoting easier.
(It is treacherous, because a command then wrongly suggests that a piece of PowerShell code is being passed, whereas the stringification simply uses the verbatim content, without expansion of variables / expressions; this ill-advised use is what created the AD filter debacle, but at this point it is hard to eradicate).
That is, user do craft commands such as the following:
The above works, because
Start-Processis a cmdlet, not an external program, so the misapplication of the-EncodedCommandargument transformation doesn't kick in - but there is no reason whybash { -c "echo hi" }shouldn't work the same (however ill-advised).
lzybkr commentedon Oct 28, 2019
This behavior also causes problems calling commands that expect strings that happen to look like a scriptblock, e.g.
I feel reasonably sure this isn't the only such tool, but I can't recall specific examples.
mklement0 commentedon Oct 28, 2019
Good point, Jason Shirk (@lzybkr), though if we consistently apply
.ToString()stringification to what are syntactically script-block arguments in string contexts (which by definition includes external-program arguments),bcdeditwould only see18472fcd-c28f-11e8-9869-8f778f73c317, which is not the intent - though a failure should still be easier to diagnose than the appearance of completely unexpected arguments.This takes us back to venerable #1761
My vote is again not to special-case the external-program behavior.
mklement0 commentedon Aug 9, 2020
Just realized that this is ultimately a duplicate of #5187.
[Make] use proper powershell script block formatting.
[Make] use proper powershell script block formatting.
[Make] use proper powershell script block formatting.
microsoft-github-policy-service commentedon Nov 16, 2023
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.
microsoft-github-policy-service commentedon Nov 16, 2023
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.
microsoft-github-policy-service commentedon Nov 16, 2023
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.
microsoft-github-policy-service commentedon Nov 23, 2023
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.