Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell using call operator on 7zip

Tags:

powershell

Intro

So, I'm attempting to read in a zip file from a directory, and unzip that to a tmp location. The problem is that powershell seems to be misinterpreting the quotes and not writing out the value of the $copyLocation variable. Instead, it is copying the files to [currentDirectory]\$copyLocation. I'm hoping you all can help. I'm new to Powershell, and this is only a piece of the script I have to write to take over our build automation. Thank you in advance.

By the way, hosts is an XmlElement which we are iterating over. So, we're deploying this zip file to a location on a number of hosts specified in an xml configuration file.

function copyWebappToDestination($copyWebappFrom,$to,$hosts){
foreach( $it in $hosts ){
    Write-Host "Deploying webapp" $webapps.webappName "to" $it.hostIp "at" $it.to

    setUpDirectories

    foreach($file in $filelocation){
        if($file.name.ToString() -eq "Web.Main.zip"){
            $filename = $file.name.ToString()
            $hostIp = $it.hostIp
            $fileDest = $file.FullName
            #unzip the files
            & '7z' x ""$fileDest"" -o""$copyLocation"" -y -r
            break
        }
    }
}

Edit

The weirdest part, is that I wrote out the command

& '7z' x "C:\MoveToProduction\SM\PROD\Web.Main.zip" -o"C:\MoveToProduction\SM\PROD\tmp" -y -r"

And ran that from a Powershell console, and it worked just fine. It just doesn't work from within the context of a script.

Edit Final

So, I gave up trying to get the call operator working. Basically, what I ended up settling on was the command:

Invoke-Expression -command  "cmd /C '7z x `"$fileDest`" -o`"$copyLocation`" -y -r'"
like image 527
Kyle Richter Avatar asked Jan 26 '26 21:01

Kyle Richter


1 Answers

So, I gave up trying to get the call operator working. Basically, what I ended up settling on was the command:

Invoke-Expression -command  "cmd /C '7z x `"$fileDest`" -o`"$copyLocation`" -y -r'"
like image 55
Kyle Richter Avatar answered Jan 28 '26 21:01

Kyle Richter