问题
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
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...
回答1:
Try something like this:
& "$sz" x $argument_0 "-o$argument_1"
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 $).
来源:https://stackoverflow.com/questions/12356869/invoke-executable-w-parameters-from-powershell-script