Passing a ScriptBlock (in syntax only) to native commands produces surprising results
See original GitHub issueSteps 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
Issue Analytics
- State:
- Created 6 years ago
- Reactions:4
- Comments:8 (7 by maintainers)
Top Results From Across the Web
about Script Blocks - PowerShell
In the PowerShell programming language, a script block is a collection of statements or expressions that can be used as a single unit....
Read more >How do I pass a scriptblock as one of the parameters in ...
Here's one way to solve this, pass the scriptblock code as a string, then create a scriptblock from the string inside the job...
Read more >How to Implement PowerShell Dynamic Prompts — Part II
The scriptblock for this command makes a call to SQL Server for the list of databases, and uses the $wordToComplete variable to restrict...
Read more >PowerShell Multithreading: A Deep Dive
Multithreading is a way to run more than one command at a time. ... isn't anything to multithread because only one command is...
Read more >Passing $variables to an Invoke-Command scriptblock?
So I've got a script that I'm trying to invoke-commands on a remote host while using a couple variables gathers by read-host.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’m suggesting syntax doesn’t matter - if the value is a
ScriptBlock
, pass the literal text unless callingpowershell.exe
orpwsh.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 whitespaceCould we just limit the encoding to script blocks following
-Command
or-c
? (Unfortunately,-c
is rather common, but we must support it anyway.)