在Azure自动化中解压缩.7z文件

时间:2015-04-01 03:24:08

标签: azure zip 7zip azure-automation

我可以在Powershell ISE(工作流程)中成功解压缩.7z,但是当我在Azure Runbook中使用相同的代码时,没有任何反应:

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)"
    }
}

调用Runbook:

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

输出:

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

正如您所看到的,.7z(test.txt)中包含的文件未被提取。

这些文件位于自动化主机的C:\ Temp文件夹中(我从blob存储中下载了它们)。我已经仔细检查过密码与用于压缩.7z文件的密码相同。 test.7z文件包含一个名为test.txt的文件。 7za.exe是7zip的可移植exe,在Powershell ISE中运行时工作正常。

1 个答案:

答案 0 :(得分:1)

事实证明,您无法在自动化主机上运行.exe文件。我下载了SevenZipSharp并将.dll文件从blob存储下载到自动化主机的C:\ Temp中,然后只使用Add-Type导入程序集,并从那里运行代码。