问题描述
我正在从 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-Zip7z.exe")) {throw "C:Program Files (x86)7-Zip7z.exe needed"}
set-alias sz "C:Program Files (x86)7-Zip7z.exe"
$argument_1 = "c: empDeployTemp"
$argument_0 = "c: empReleaseWeb_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 脚本调用可执行文件(带参数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!