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

larryq
  • 15,713
  • 38
  • 121
  • 190

1 Answers1

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

Goyuix
  • 23,614
  • 14
  • 84
  • 128
  • 1
    Thank you. The update that worked for me was & sz x $argument_0 "-o$argument_1" – larryq Sep 10 '12 at 19:58
  • 2
    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. – latkin Sep 10 '12 at 20:04
  • 1
    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"` – latkin Sep 10 '12 at 20:07