[英]Double quotes not escaping from string when calling Powershell command from within Batch script?
Double quotes structure is not retained in my test message when passed through a powershell instance called through a batch script, as detailed below: 通过批处理脚本调用的Powershell实例传递时,双引号结构未保留在测试消息中,如下所示:
set test={"this":"is","a":"test"}
FOR /F "delims=" %%i in (' powershell -Command "& {$message = '%test%'; echo $message}" ') DO SET message=%%i
echo %test%
echo %message%
the output is as follows: 输出如下:
{"this":"is","a":"test"}
{this:is,a:test}
I would like to retain the quotes to further process the string in powershell, but as you can see, they are stripped when introduced into the $message
variable. 我想保留引号以进一步在powershell中处理字符串,但是如您所见,将引号引入
$message
变量后会删除它们。
Any insight as to how I might fix this? 关于如何解决此问题的任何见解?
Echoing %test%
containing double quotes inside the d-quoted powershell command will break the enclosing d-quotes. 在d-引用的powershell命令中回显包含双引号的
%test%
会破坏包围的d-引号。
One way to overcome this, is using batch string substitution to escape the inner d-quotes with a backslash on the fly 解决此问题的一种方法是使用批处理字符串替换以动态反斜线转义内部d引号
:: Q:\Test\2019\04\25\SO_55855412.cmd
@Echo off
set "test={"this":"is","a":"test"}"
FOR /F "delims=" %%i in ('
powershell -Command "& {$message = '%test:"=\"%'; echo $message}"
') DO SET "message=%%i"
echo %test%
echo %message%
returns here: 返回此处:
> SO_55855412.cmd
{"this":"is","a":"test"}
{"this":"is","a":"test"}
Another solution is to NOT pass %test%
as an argument, but to get the variable from the inherited environment 另一个解决方案是不通过
%test%
作为参数,而是从继承的环境中获取变量。
powershell -Command "& {$message = $env:test; echo $message}"
Sadly this doesn't work to pass a variable back, as on terminating the spawned app the inherited environment is discarded. 遗憾的是,这无法将变量传递回来,因为在终止生成的应用程序时,继承的环境将被丢弃。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.