简体   繁体   English

从批处理文件执行powershell命令时转义双引号

[英]Escaping double quotes while executing powershell commands from batch file

I have to use replace command with regex in the PowerShell command.我必须在 PowerShell 命令中使用带有正则表达式的替换命令。 Replace with RegEx works only with double quotes ("). Also to execute the PowerShell command in batch file, the command must be inside double quotes (") like this:替换为 RegEx 仅适用于双引号 (")。此外,要在批处理文件中执行 PowerShell 命令,该命令必须在双引号 (") 内,如下所示:

powershell -command ""

So, is there a way to escape the double quotes used with the replace command as the below is throwing error because of conflict in double quotes:那么,有没有办法转义与替换命令一起使用的双引号,因为下面由于双引号中的冲突而引发错误:

powershell -command "$content -replace ",$", ",NULL""

I have tried using (`) like:我试过使用 (`) 像:

powershell -command "$content -replace `",$`", `",NULL`""

Could anyone please suggest some solution?任何人都可以请提出一些解决方案吗?

Use twice the double quote symbol (ie "") to escape it in PowerShell.在 PowerShell 中使用两次双引号(即“”)来对其进行转义。

echo "Here is my ""double quoted"" test."

This will print: Here is my "double quoted" test.这将打印:这是我的“双引号”测试。

Did you try something like:你有没有尝试过:

-command "$content -replace "",$"", "",NULL"""

这是一个迟到的响应,但是您可以在双引号之间使用'引号,然后您可以这样写: powershell -command "$content -replace ',$', ',NULL'"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM