PowerShell Executing a function within a Script Block using Start-Process does weird things with double quotes

时间秒杀一切 提交于 2019-12-04 06:29:35
TessellatingHeckler

If I change your script to be

-Command $ScriptBlock

Run it, and have it open a new shell window, then run

gci function:test | fl 

to see the function definition in the new window, the code shown is

$status = This is a string

with the same test on the single quote version it shows

$status = 'This is a string'

So it's losing the double quotes. Escape them with double quotes

$status = """This is a string"""

and they come through OK. Also even though scriptblocks are compiled code, it looks to me like they are embedded as text if you expand them into a string:

> $s = { "hello" }
> "---$s---"
---"hello"---

So I think you're hitting this kind of quoting problem: PowerShell stripping double quotes from command line arguments and the answer by Droj particularly, saying "The weird thing about sending parameters to external programs is that there is additional level of quote evaluation. I don't know if this is a bug, but I'm guessing it won't be changed, because the behavior is the same when you use Start-Process and pass in arguments.".

PowerShell is expanding the script block as a string into your command, then those single quotes around the string are being reinterpreted as quoted parameters and removed somewhere in the invoking. Which is either a known problem, or a bug, or by-design, depending on how you read that linked connect article.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!