我在PowerShell中将字符串数组传递给一个命令时遇到了一些问题,所以我正在调试脚本。我正在使用在EchoArgs.exe中找到的PowerShell社区扩展项目程序。如果我执行这个脚本:
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 (即cmdlet)中是否有一种“智能”方法来转义任何字符串以避免此类问题?
发布于 2016-03-05 16:55:45
尝试使用Start-Process代替。它有一个更适合这种情况的$Arguments参数。
https://stackoverflow.com/questions/35750665
复制相似问题