我可以在 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)"
}
}
调用运行手册:
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 中运行时运行良好。