Invoke executable (w/parameters) from powershell s

2019-06-24 03:36发布

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条回答
地球回转人心会变
2楼-- · 2019-06-24 03:59

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 $).

查看更多
登录 后发表回答