David Klempfner
David Klempfner

Reputation: 9930

Unzipping a .7z file in Azure Automation

I can successfully unzip a .7z in Powershell ISE (workflow), however when I use the same code in an Azure runbook, nothing happens:

workflow Unzip-File
{
    Param([Parameter(mandatory=$true)][String]$zipFileSource,
          [Parameter(mandatory=$true)][String]$destinationFolder,
          [Parameter(mandatory=$true)][String]$password,
          [Parameter(mandatory=$true)][String]$pathTo7zipExe)

    InlineScript
    {
        Write-Output "${using:zipFileSource} exists? - $(Test-Path ${using:zipFileSource})"
        Write-Output "${using:destinationFolder} exists? - $(Test-Path ${using:destinationFolder})"
        Write-Output "${using:pathTo7zipExe} exists? - $(Test-Path ${using:pathTo7zipExe})"
        $passwordSwitch = "-p" #this is needed because otherwise the password is literally $password rather than the string stored in that variable.
        $destinationDirSwitch = "-o"
        & ${using:pathTo7zipExe} x ${using:zipFileSource}$destinationDirSwitch${using:destinationFolder}$passwordSwitch${using:password} -y #-y means if prompted for yes/no, choose yes automatically.

        $fileName = "test.txt"
        $destinationPath = [System.IO.Path]::Combine(${using:destinationFolder}, $fileName)
        Write-Output "$destinationPath exists? - $(Test-Path $destinationPath)"
    }
}

Calling the runbook:

Unzip-File `
        -destinationFolder C:\Temp `
        -Password "ThePassword" `
        -pathTo7zipExe 'C:\Temp\7za.exe' `
        -zipFileSource 'C:\Temp\test.7z'

Output:

C:\Temp\test.7z exists? - True
c:\temp exists? - True
C:\Temp\7za.exe exists? - True
c:\temp\test.txt exists? - False

As you can see the file contained within the .7z (test.txt) is not being extracted.

The files are on the Automation host's C:\Temp folder (I downloaded them there from blob storage). I have double checked that the password is the same one that was used to zip the .7z file. The test.7z file contains one file called test.txt. 7za.exe is the portable exe for 7zip and worked fine when run in Powershell ISE.

Upvotes: 1

Views: 2236

Answers (1)

David Klempfner
David Klempfner

Reputation: 9930

So turns out you can't run .exe files on the automation host. I downloaded SevenZipSharp and downloaded the .dll files from blob storage into the Automation host's C:\Temp, then just used Add-Type to import the assembly, and ran the code from there.

Upvotes: 1

Related Questions