question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:4
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
lzybkrcommented, Oct 21, 2017

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

0reactions
yecril71plcommented, Aug 21, 2020

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found