2015-04-01 90 views
1

我可以成功解壓一個.7z壓縮在PowerShell中ISE(工作流),但是當我在Azure中運行手冊使用相同的代碼,沒有任何反應:解壓在Azure的自動化.7z壓縮文件

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

調用運行手冊:

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

原來你不能在自動化主機上運行.exe文件。我下載了SevenZipSharp並將.dll文件從blob存儲下載到了自動化主機的C:\ Temp中,然後使用Add-Type導入程序集,然後從那裏運行代碼。

+2

如果您不想管理Azure Blob存儲中的文件或每次運行Runbook時都必須從blob存儲下載.dlls,則可以將SevenZipSharp的.dlls包裝到PowerShell模塊中,並將該模塊導入Azure自動化。 – Joe 2015-04-03 03:01:20