0
我有一些问题传递一个字符串数组到Powershell中的命令,所以我正在调试我的脚本。我正在使用PowerShell Community Extension Project (PSCX)中的EchoArgs.exe程序。 如果我执行这个脚本:如何在命令行参数中正确地转义空格和反斜杠?
Import-Module Pscx
cls
$thisOne = 'this_one\';
$secondOne = 'second one\';
$lastOne = 'last_one'
$args = $thisOne `
, "the $secondOne" `
, "the_$lastOne"
EchoArgs $args
我得到这样的结果:
Arg 0 is <this_one\>
Arg 1 is <the second one" the_last_one>
Command line:
"C:\Program Files (x86)\PowerShell Community Extensions\Pscx3\Pscx\Apps\EchoArgs.exe" this_one\ "the second one\" the_last_one
看来,如果一个字符串包含空格,最后一个反斜杠转义双引号。其实一切似乎工作,如果我逃只有反斜线:
Import-Module Pscx
cls
$thisOne = 'this_one\';
$secondOne = 'second one\\';
$lastOne = 'last_one'
$args = $thisOne `
, "the $secondOne" `
, "the_$lastOne"
EchoArgs $args
这个结果:
Arg 0 is <this_one\>
Arg 1 is <the second one\>
Arg 2 is <the_last_one>
Command line:
"C:\Program Files (x86)\PowerShell Community Extensions\Pscx3\Pscx\Apps\EchoArgs.exe" this_one\ "the second one\\" the_last_one
所以我的问题是:是否有PowerShell中的一个“聪明”的方式(即小命令),以逃避任何字符串以避免此类问题?
关于你的PS脚本中使用单引号是什么?所以'$ args ='this_one',''第二个'','the_last_one'' – romellem
你说得对,但我必须在数组字符串中使用变量,比如'$ args =“$ thisOne”'等等。 – lucazav
我刚刚更新了代码片段。谢谢 – lucazav