powershellpowershell-3.0powershell-4.0

Can't extract zip with powerhell


I need to update 100+ servers to the latest VBR patches, I wrote a script for that, it's working OK until it gets to the part of extracting the file, it says that it extracted the file but when I try to run it I got storage errors, than I tried to extract it only with the expand-archive outside of the script, I receive the same strange error

Here is the code:

#$VeeamVersion = Get-ItemProperty "HKLM:\Software\Veeam\Veeam Backup and Replication" | Select-Object -ExpandProperty DisplayVersion
$ProgressPreference = 'SilentlyContinue'
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$VeeamVersion = (Get-Item 'C:\Program Files\Veeam\Backup and Replication\Backup\Packages\VeeamDeploymentDll.dll').VersionInfo |select -ExpandProperty FileVersion

if ($VeeamVersion -eq "11.0.1.1261") {
    Write-Host "Veeam Backup and Replication version 11.0.1.1261 is installed on your PC"
    $DownloadUrl = "https://download2.veeam.com/VBR/v11/VeeamBackup&Replication_11.0.1.1261_20230227.zip"
} elseif ($VeeamVersion -eq "12.0.0.1420") {
    Write-Host "Veeam Backup and Replication version 12.0.0.1420 is installed on your PC"
    $DownloadUrl = "https://download2.veeam.com/VBR/v12/VeeamBackup&Replication_12.0.0.1420_20230412.zip"
} else {
    Write-Host "Neither Veeam Backup and Replication version 11.0.1.1261 nor version 12.0.0.1420 is installed on your PC"
}

if ($DownloadUrl) {
    New-Item -Path d:\ -name "veeam_update" -ItemType "Directory"
    $DownloadPath = "d:\Veaam_update"

    if (Test-Path "$DownloadPath\veeam*.zip") {
        Write-Host "Veeam Backup and Replication is already downloaded"
    } else {
        Write-Host "Downloading Veeam Backup and Replication..."
        Invoke-WebRequest -Uri $DownloadUrl -OutFile "$DownloadPath\veeam.zip"
        Expand-Archive -LiteralPath "$DownloadPath\veeam.zip" -DestinationPath "$DownloadPath"
        Write-Host "Veeam Backup and Replication downloaded successfully"
    }

    Write-Host "Installing Veeam Backup and Replication silently..."
    #Start-Process "D:\Veaam_update\*.exe" -ArgumentList "/S" -Wait
    Write-Host "Veeam Backup and Replication installed successfully"
}

here is the error:

Exception calling "ExtractToFile" with "3" argument(s): "The archive entry was compressed using an unsupported compression method."
At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:1059 char:25
+ ...             [System.IO.Compression.ZipFileExtensions]::ExtractToFile( ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : InvalidDataException

Solution

  • I was able to fix the issue, here is the fixed and working code:

    This code will do:

    1. Check if Veeam is running.
    2. Check if you have 7zip installed
    3. Check which version of Veeam Backup And replication you have - and will download the updated according to the version.
    4. It will then create a new folder named "veeam_update" on drive d:
    5. The file will be downloaded to that folder.
    6. Using 7zip it will extract the zip file to the same folder
    7. It will run the installation silently
    8. It will update components of Veeam

    Enjoy.

    #run script as admin
    if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; exit }
    
    #Set exectution policy
    Set-ExecutionPolicy -ExecutionPolicy Undefined -Scope CurrentUser -Confirm:$false -Force
    
    $ProgressPreference = 'SilentlyContinue'
    
    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    
    
    # Check if Veeam Backup and Replication Shell is running
    $veeamShell = Get-Process -Name "veeam.backup.shell" -ErrorAction SilentlyContinue
    if ($veeamShell) {
        Write-Host "Closing Veeam Backup and Replication Shell..."
        Stop-Process -Name "veeam.backup.shell" -Force -Confirm:$false
    }
    
    
    
    
    # Check if 7-Zip is installed
    if (!(Test-Path "C:\Program Files\7-Zip\7z.exe")) {
        # Download 7-Zip
        $url = "https://www.7-zip.org/a/7z2201-x64.exe"
        $output = "$env:TEMP\7z2201-x64.exe"
        Invoke-WebRequest -Uri $url -OutFile $output
    
        # Install 7-Zip silently
        Start-Process -FilePath $output -ArgumentList "/S" -Wait
    }
    
    #start veeam installation
    $VeeamVersion = (Get-Item 'C:\Program Files\Veeam\Backup and Replication\Backup\Packages\VeeamDeploymentDll.dll').VersionInfo |select -ExpandProperty FileVersion
    
    if ($VeeamVersion -eq "11.0.1.1261") {
        Write-Host "Veeam Backup and Replication version 11.0.1.1261 is installed on this Server"
        $DownloadUrl = "https://download2.veeam.com/VBR/v11/VeeamBackup&Replication_11.0.1.1261_20230227.zip"
    } elseif ($VeeamVersion -eq "12.0.0.1420") {
        Write-Host "Veeam Backup and Replication version 12.0.0.1420 is installed on this Server"
        $DownloadUrl = "https://download2.veeam.com/VBR/v12/VeeamBackup&Replication_12.0.0.1420_20230412.zip"
    } else {
        Write-Host "Neither Veeam Backup and Replication version 11.0.1.1261 nor version 12.0.0.1420 is installed on your PC"
    }
    
    if ($DownloadUrl) {
        New-Item -Path d:\ -name "veeam_update" -ItemType "Directory"
        $DownloadPath = "d:\veeam_update"
    
        if (Test-Path "$DownloadPath\veeam*.zip") {
            Write-Host "Veeam Backup and Replication is already downloaded"
        } else {
            Write-Host "Downloading Veeam Backup and Replication..."
            Invoke-WebRequest -Uri $DownloadUrl -OutFile "$DownloadPath\veeam.zip"
            & "C:\Program Files\7-Zip\7z.exe" x "$DownloadPath\veeam.zip" -o"$DownloadPath"
            Write-Host "Veeam Backup and Replication downloaded successfully"
        }
    
        Write-Host "Installing Veeam Backup and Replication silently..."
        Start-Process "D:\veeam_update\*.exe" -ArgumentList "/Silent" -Wait
        Write-Host "Veeam Backup and Replication installed successfully"
    }
    
    
    #update veeam components:
    Write-Host "Updateing Veeam Backup and Replication components "
    Update-VBRServerComponent
    
    Write-Host "all done "
    pause