2012-09-10 65 views
4

我打电话给一个来自powershell的zip实用程序,并且很难得到其参数。代码如下:从powershell脚本调用可执行文件(带参数)

if (-not (test-path "C:\Program Files (x86)\7-Zip\7z.exe")) {throw "C:\Program Files (x86)\7-Zip\7z.exe needed"} 
    set-alias sz "C:\Program Files (x86)\7-Zip\7z.exe" 

    $argument_1 = "c:\temp\DeployTemp\" 
    $argument_0 = "c:\temp\Release\Web_Feature_2012R10_1_1112.prod.com.zip" 

    sz x $argument_0 -o$argument_1 

问题是7zip可执行调用字面提取到名为$ argument_1的目录,而不是存储在字符串中的实际值。我试过用几种方式逃避价值,但没有运气。不幸的是,7zip的“O”标志不能把它和输出目录之间的空间......

回答

5

尝试是这样的:

& "$sz" x $argument_0 "-o$argument_1" 

的符号告诉PowerShell来对待表达更像CMD .exe会,但仍然允许变量扩展(以$开头的令牌)。

+1

谢谢。对我有效的更新是&sz x $ argument_0“-o $ argument_1” – larryq

+2

&符号不会导致行为“更接近cmd”。它仅仅向powershell表明即将到来的*字符串*应该被解释为*命令*。这允许用户通过具有空格或特殊字符的完整路径来调用可执行文件或脚本。对其余的参数没有影响。 – latkin

+1

修复只是为了获得逃脱权利。最后一个参数只需要加上'“-o $ argument_1”',因为双引号允许变量扩展。完整的命令可以是'sz x $ argument_0“-o $ argument_1”' – latkin

相关问题