简体   繁体   English

从批处理脚本中调用Powershell命令时,双引号是否不能从字符串转义?

[英]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.

相关问题 从批处理文件执行powershell命令时转义双引号 - Escaping double quotes while executing powershell commands from batch file 如何使用 powershell 命令用新行替换字符串? 从批处理文件中? - 双引号问题 - How can I make use a powershell command to replace a string witha new line? From within a batch file? -double quotes issue 将%*从批处理脚本作为$ args传递到Powershell脚本时转义参数 - Escaping arguments when passing %* from batch script as $args to powershell script 将string []从批处理文件(其中包含双引号“)传递到powershell脚本 - Passing string[] from batch file (which contains double quotes ") to powershell script 批处理文件发出的 powershell 命令中的 Escaping 引用 - Escaping quotes in powershell command issued by batch file 在批处理文件中调用Powershell脚本时,如何使用嵌套引号? - How can I use nested quotes when calling a powershell script within a batch file? 在引号中的字符串中转义@ - escaping @ in a string in quotes powershell 从批处理脚本调用Powershell脚本 - Calling a powershell script from Batch scripting 从批处理文件调用PowerShell脚本 - Calling a PowerShell script from a batch file PowerShell - escaping 花式单引号和双引号,用于正则表达式和字符串替换 - PowerShell - escaping fancy single and double quotes for regex and string replace
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM