4

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 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. The update that worked for me was & sz x $argument_0 "-o$argument_1"
The ampersand does nothing to cause behavior "closer to cmd." It merely indicates to powershell that the upcoming string should be interpreted as a command. This allows the user to invoke executables or scripts via a full path with spaces or special characters. There is no effect on the remaining arguments.
The fix was simply to get the escaping right. The final argument just needs to be "-o$argument_1" as double quotes allow for variable expansion. The full command could be sz x $argument_0 "-o$argument_1"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.