You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Passing a ScriptBlock (in syntax only) to native commands produces surprising results #5187
Breaking-Changebreaking change that may affect usersIssue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-No ActivityIssue has had no activity for 6 months or more
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.
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
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?
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.
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
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.
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 commented on 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.
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 commented on Nov 23, 2023
Breaking-Changebreaking change that may affect usersIssue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-No ActivityIssue has had no activity for 6 months or more
Activity
SteveL-MSFT commentedon Oct 21, 2017
So the expected behavior is that if the command is native, braces are literal always? What about parenthesis?
lzybkr commentedon Oct 21, 2017
I'm suggesting syntax doesn't matter - if the value is a
ScriptBlock, pass the literal text unless callingpowershell.exeorpwsh.exe, then use-encodedCommand.We could attempt to be smart too - only pass the literal if the
ScriptBlockhas no whitespace, or I suppose escaped whitespaceSteveL-MSFT commentedon Oct 25, 2017
Isn't this a dupe of #1761?
lzybkr commentedon Oct 26, 2017
Not really a dupe.
BrucePay commentedon Mar 9, 2018
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?
lzybkr commentedon Mar 9, 2018
Bruce Payette (@BrucePay) -
bcdeditaccepts 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.
mklement0 commentedon Oct 9, 2018
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&incmd.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.
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:
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).
yecril71pl commentedon Aug 21, 2020
Could we just limit the encoding to script blocks following
-Commandor-c? (Unfortunately,-cis rather common, but we must support it anyway.)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.