您好,我在執行 powershell 時遇到問題,例如:
cmd = EvalInsert(
"\[$imageFile = "test"]\");
rp1 = RunProgram(Executable("C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" ),
Options(Eval(cmd)),
ReadFunction("text")
);我想使用的實際命令更複雜,但與語音標記有同樣的問題。 在日誌中打印 rp1 給出:
"test : The term 'test' is not recognized as the name of a cmdlet, function, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:14
+ $imageFile = test
+ ~~~~
+ CategoryInfo : ObjectNotFound: (test:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
" 這意味著該線路是在沒有語音標記的情況下運行的。
如何確保語音標記正確運行?
預先感謝您對此的任何幫助。
剛剛偶然發現了關於 Windows 命令列、引號、轉義的精彩解釋,以及為什麼它看起來如此困難。
http://www.windowsinspired.com/the- Correct-way-to-quote-command-line-arguments/
http://www.windowsinspired.com/understanding-the-command-line-string-and-arguments-received-by-a-windows-program/
http://www.windowsinspired.com/how-a-windows-programs-splits-its-command-line-into-individual-arguments/
過於簡化:命令列上的多個參數以空格分隔。每個引號都會切換是否使用空格來分隔參數。 \\ 是一個文字 \ 並且 \" 是一個文字 "。當空格關閉時,兩個相鄰的引號會產生文字 ",但空格可能會開啟或關閉,這取決於使用的解析器變體。
2025 年 1 月更新:連結已過時,網頁存檔版本:
http://web.archive.org/web/20220629215303/http://windowsinspired.com/the- Correct-way-to-quote-command-line-arguments/
https://web.archive.org/web/20220629212707/https://www.windowsinspired.com/understanding-the-command-line-string-and-arguments-received-by-a-windows-program/
https://web.archive.org/web/20220629212422/https://www.windowsinspired.com/how-a-windows-programs-splits-its-command-line-into-individual-arguments/
命令行參數需要一些額外的引號和轉義才能使 powershell 命令行解析工作。 (這是我第二次使用powershell,我可能忽略了一些東西......) (此外,此網頁語法著色對於方括號引用轉義來說是錯誤的。)
command = "\["$imagefile = \"test\";echo $imagefile"]\";
Write( "\!nthe command is:", command ); // write shows it in the log without extra escapes
result = RunProgram(
executable( "powershell.exe" ), //
options( command ), //
readfunction( "text" )//
);
Show( result );
命令是:"$imagefile = \"test\";echo $imagefile"
結果=“測試
”;
這是我在 CMD 窗口中進行的兩次嘗試的屏幕截圖。
powershell output in a CMD windowpowershell output in a CMD windowpowershell output in a CMD windowCMD 窗口中的 powershell 輸出
外部引號使 powershell 看到單個選項;在 powershell 獲取文本之前,CMD 會刪除外部引號。 在您的原始示例中,powershell 收到了三個項目($imagefile、=、test),並且 test 周圍的外部引號已被 CMD 刪除。 當參數中有引號或空格時,需要使用外部引號。