Download Firefox

I’ve been using the ftp site of Mozilla for a few years to download Firefox releases and check their hashes.
Unfortunately, Mozilla decommissioned that site and left the following instructions on this page https://ftp.mozilla.org/pub/firefox/releases/latest/README.txt
MF-ftp

Here’s the function I quickly wrote to address these shortcomings:

#Requires -Version 3.0

Function Download-Firefox {
[CmdletBinding()]
Param(

[Parameter(Mandatory)]
[ValidateSet('Win32','Win64','OSX','Linux32','Linux64')]
[string]$TargetOS,

[Parameter()]
[ValidateSet('fr','en-US','en-GB')]
[string]$Language = 'en-GB',

[Parameter(Mandatory)]
[string]$TargetFolder

)
Begin {

    try {
        Get-Item $TargetFolder -ErrorAction Stop | Out-Null
    } catch {
        Write-Warning -Message "The target folder specified as parameter does not exist"
        break
    }

    Switch ($TargetOS) {
        'Win32'   { $OS = 'win'     ; break }
        'Win64'   { $OS = 'win64'   ; break }
        'OSX'     { $OS = 'osx'     ; break }
        'Linux32' { $OS = 'linux'   ; break }
        'Linux64' { $OS = 'linux64' ; break }
        default   {}
    }

    $URI = ('https://download.mozilla.org/?product=firefox-latest&os={0}&lang={1}' -f $OS,$Language)
    Write-Verbose -Message "Download URL set to $($URI)"
}
Process {

    $page  = Invoke-WebRequest -Uri $URI -MaximumRedirection 0 -ErrorAction SilentlyContinue -UseBasicParsing

    if ($page.StatusCode -eq 302) {

        $FileSource = $page.Headers.Location
        $FileName =  Split-Path ([uri]$page.Headers.Location).LocalPath -Leaf

        if (Test-Path -Path $TargetFolder -PathType Container) {
            try {
                Invoke-WebRequest -Uri $FileSource -UseBasicParsing -OutFile (Join-Path -Path $TargetFolder -ChildPath $FileName) -ErrorAction Stop
                Write-Verbose -Message "Successfully downloaded $($FileName)  from $($FileSource)"
                Get-Item (Join-Path -Path $TargetFolder -ChildPath $FileName)
            } catch {
                Write-Warning -Message "Failed to download $($FileName) from $($FileSource) because $($_.Exception.Message)"
                break
            }
        } else {
            Write-Warning -Message "The target folder specified as parameter should be a folder"
        }
    } else {
        Write-Warning -Message "Failed to query $URI. Return code was $($page.StatusCode)"
    }
}
End {}
} # end of function

Here’s how to use this function:

Download-Firefox -TargetOS Win64 -Language en-US -TargetFolder C:\ -Verbose |
Get-AuthenticodeSignature

Download-MF

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s