Jump to content

Install 7-Zip with PowerShell


kavaa

Recommended Posts

I made a install script for 7-Zip to do a Silent install and clean if needed.

Improvements are welcome!

When a new version of 7-Zip comes out, just change the 7z1604-x64.msi to the new value.

# Silent Install 7-Zip
# http://www.7-zip.org/download.html 

# Path for the workdir
$workdir = "c:\installer\"

# Check if work directory exists if not create it

If (Test-Path -Path $workdir -PathType Container)
{ Write-Host "$workdir already exists" -ForegroundColor Red}
ELSE
{ New-Item -Path $workdir  -ItemType directory }

# Download the installer

$source = "http://www.7-zip.org/a/7z1604-x64.msi"
$destination = "$workdir\7-Zip.msi"

# Check if Invoke-Webrequest exists otherwise execute WebClient

if (Get-Command 'Invoke-Webrequest')
{
     Invoke-WebRequest $source -OutFile $destination
}
else
{
    $WebClient = New-Object System.Net.WebClient
    $webclient.DownloadFile($source, $destination)
}

Invoke-WebRequest $source -OutFile $destination 

# Start the installation

msiexec.exe /i "$workdir\7-Zip.msi" /qn

# Wait XX Seconds for the installation to finish

Start-Sleep -s 35

# Remove the installer

rm -Force $workdir\7*

 

Link to comment
Share on other sites

  • 1 year later...

The Script now installs the latest Version of 7 Zip by by reading the Releasenumber from SourceForge. It now Accepts Spaces at the workdirectory, and writes What its Doing to the User.

 

 

#Requires -Version 5.0

Write-Verbose "Getting latest Version" -Verbose

class SourceForge {
    [string]        $Project = $Null
    [PSCustomObject]$LatestRelease = $Null

    
    SourceForge([string] $project) {
        $this.Project = $project
        $this.GetLatestRelease()
    }

    
    [void] GetLatestRelease() {
        $originalSecurityProtocol = [Net.ServicePointManager]::SecurityProtocol
        [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
        
        $this.GetLatestRelease('https://sourceforge.net/projects/{0}/best_release.json')
        
        [Net.ServicePointManager]::SecurityProtocol = $originalSecurityProtocol
    }


    [void] GetLatestRelease([string] $url) {
        $url = $url -f @($this.Project)
        Write-Debug "[SourceForge].GetLatestRelease URL: ${url}"
        $this.LatestRelease = ConvertFrom-Json (Invoke-WebRequest $url -UseBasicParsing).Content
    }

    
    [string] LatestVersion() {
        if (-not $this.LatestRelease) {
            $this.GetLatestRelease()
        }

        return $this.LatestRelease.release.filename.Split('/')[2]
    }

    [hashtable] LatestHash() {
        if (-not $this.LatestRelease) {
            $this.GetLatestRelease()
        }

        return @{
            'Algorithm' = 'MD5';
            'Hash' = $this.LatestRelease.release.md5sum.ToUpper();
        }
    }
}

$7zip = [SourceForge]::new('sevenzip')

$Version = $7zip.LatestVersion() 

# get Numbers from String

$Version = $Version -replace("[^\d]","")



# Silent Install 7-Zip
# http://www.7-zip.org/download.html 

# Path for the workdir
$workdir = 'C:\Installer'

Write-Verbose "Creating Working Directory: $workdir" -Verbose

# Check if work directory exists if not create it

If (!(Test-Path -Path $workdir -PathType Container))
{ New-Item -Path $workdir  -ItemType directory }

# Download the installer
Write-Verbose "Downloading 7-Zip Version $Version" -Verbose

$source = "http://www.7-zip.org/a/7z$Version-x64.exe"

$destination = "$workdir\7-Zip.exe"

# Check if Invoke-Webrequest exists otherwise execute WebClient

if (Get-Command 'Invoke-Webrequest')
{
     Invoke-WebRequest $source -OutFile $destination
}
else
{
    $WebClient = New-Object System.Net.WebClient
    $webclient.DownloadFile($source, $destination)
}

Invoke-WebRequest $source -OutFile $destination 

# Start the installation

Write-Verbose "Install 7-Zip Version $Version" -Verbose

$UnattendedArgs='/S'
$File='"' + $workdir + '\7-zip.exe"'

(Start-Process $File $UnattendedArgs -Wait -Passthru).ExitCode

# Remove the installer

Write-Verbose "Delete Working Directory" -Verbose

Remove-Item $workdir -Recurse -Force 

 

 

 

Link to comment
Share on other sites

  • 4 weeks later...

If you look into using chocolatey, it works quite nicely in Pulseway to install apps:

choco install 7zip

You would have to install chocolatey on the endpoint, but then you can use it to script installs, uninstalls and change Windows settings as well.

Link to comment
Share on other sites

  • 3 months later...

Hay BartB, I agree, I followed your advise and it works well, I'm worried however about the source files, It would be very cool if a group of us Pulseway users worked to as a team to setup our own file hosting of secure files, interested in starting something up?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...