从Powershell脚本调用可执行文件(带有参数) [英] Invoke executable (w/parameters) from powershell script

查看:747
本文介绍了从Powershell脚本调用可执行文件(带有参数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Powershell调用zip实用程序,并且很难直接获取其参数。代码如下:

I'm calling a zip utility from powershell and having a difficult time getting its parameters straight. Here's the code:

    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标志和输出目录之间不能有空格...

The problem is the 7zip executable call literally extracts to a directory named $argument_1, instead of the actual value stored in the string. I've tried escaping the value in a few ways but without luck. Unfortunately the 7zip "-o" flag can't have a space between it and the output directory...

推荐答案

尝试一些操作像这样:

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

&符告诉PowerShell处理表达式就像CMD.exe一样,但仍然允许变量扩展(以$开头的令牌)。

The ampersand tells PowerShell to treat the expression more like CMD.exe would, but still allow for the variable expansion (tokens that start with a $).

这篇关于从Powershell脚本调用可执行文件(带有参数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆