您好,我在执行 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/
过于简单:命令行上的多个参数用空格分隔。每个引号切换是否使用空格来分隔参数。 \\ 是文字 \ 而 \" 是文字 "。当空格被关闭时,两个相邻的引号会变成文字 ",但空格可能会根据所使用的解析器变体而打开或关闭。
更新 20jan2025:链接已失效,网络存档版本:
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 删除。 当参数中有引号或空格时,需要使用外部引号。