2016-06-13 81 views
1

我嘗試在批處理文件中調用PowerShell命令:PowerShell的命令,其中雙引號是必需的命令裏面

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

但總是失敗,此錯誤

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 

我嘗試用單引號替換字符串

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

但反引號轉義字符被對待像任何其他文本字符出現在用單引號括起來的字符串中時。

回答

1

只是逃生使用反斜槓雙引號:

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

單引號,甚至沒有必要的,逃逸的雙引號是足夠的。 –

+0

它給出了「錯誤:無法識別的命令」 –

+0

@GeraldSchneider如果我不把命令放在單引號中,我會得到一個錯誤 - 是嗎? –