批处理文件中的PowerShell命令,其中命令内需要双引号 [英] PowerShell command in batch file where double quotes are required inside the command

查看:68
本文介绍了批处理文件中的PowerShell命令,其中命令内需要双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在批处理文件中调用PowerShell命令:

I try to invoke a PowerShell command within a batch file:

powershell -Command "(gc test.txt ) -replace ("~\[","`r`n[") | sc test.txt"

但是它总是会因以下错误而失败:

But it always fails with this error:

At line:1 char:29
+ (gc test.txt ) -replace (~\[,`r`n[) | sc test.txt
+                             ~
Missing argument in parameter list.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordEx
   ception
    + FullyQualifiedErrorId : MissingArgument

我尝试用单引号引起替换字符串:

I try with single quotes for the replace strings:

powershell -Command "(gc test.txt ) -replace ('~\[','`r`n[') | sc test.txt"

但是当反引号转义字符出现在用单引号引起来的字符串中时,它会像其他任何文本字符一样被对待.

But the backtick escape character is treated like any other text character when it appears within a string enclosed in single quotes.

推荐答案

只需使用反斜杠转义即可:

powershell -Command "(gc test.txt ) -replace (\"~\[\",\"`r`n[\") | sc test.txt"

这篇关于批处理文件中的PowerShell命令,其中命令内需要双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆