Skip to content

Instantly share code, notes, and snippets.

@crpb
Last active May 18, 2023 21:46
Show Gist options
  • Save crpb/d9d06d52d67263a986790974607dae7a to your computer and use it in GitHub Desktop.
Save crpb/d9d06d52d67263a986790974607dae7a to your computer and use it in GitHub Desktop.
PDQ Deploy Firefox Auto-Download Package automatically in your own Language via a Pre-Task.
# Set the Version in which the Script is pasted.
#$ff_release = "Firefox"
$ff_release = "Firefox-ESR"
#For what purpose was this all?
$ff_lang = "de"
# Universal separator \o/
$sep = [IO.Path]::DirectorySeparatorChar
# 2023.05
# https://help.pdq.com/hc/en-us/articles/5719272144667-PDQ-Package-Library-Changelog
# PDQ changed their Auto-Download folder and filename schema
# OLD: $(Repository)\Mozilla\Firefox-ESR\x64\Firefox Setup 102.10.0esr.exe
# NEW: $(Repository)\FirefoxESR\102.11.0\FirefoxESR_64bit_102.11.0.exe
# Fifefox-Release-Source
# https://www.mozilla.org/en-US/firefox/all/#product-desktop-release
if ($ff_release -notmatch "esr") { $url, $extension, $opt = "https://download.mozilla.org/?product=firefox-msi-latest-ssl&os=win64&lang=$ff_lang", "msi", "" }
else { $url, $extension, $opt = "https://download.mozilla.org/?product=firefox-esr-latest-ssl&os=win64&lang=$ff_lang", "exe", "ESR" }
#$ff_folder = "Mozilla" + $sep + $ff_release + $sep + "x64"
$ff_folder = $ff_release.Replace('-', '')
#if (Get-Command Repository -ErrorAction Ignore) { $WorkingDir = $(Repository) + $sep + $ff_folder }
$WorkingDir = "$(Repository)" + $sep + $ff_folder
#$WorkingDir = '/home/cb/tmp/repository' + $sep + $ff_folder
#$WorkingDir = "\\nas-01\software\pdq\repo" + $sep + $ff_folder
Write-Output "workDir $WorkingDir"
$waitDays = 1
$lastDownload = $WorkingDir + $sep + "last.dl"
$HTTPFolderUrl = $url
Function getHTTPInfos {
Write-Output "getHTTPInfos:`n"
$script:HTTPRequest = [System.Net.HttpWebRequest]::Create("$HTTPFolderUrl")
$script:HTTPRequest.Method = [System.Net.WebRequestMethods+Http]::Head
$script:HTTPResponse = $HTTPRequest.GetResponse()
$script:HTTPSize = $HTTPResponse.ContentLength
$script:HTTPVersion = $HTTPResponse.ResponseUri.localpath.Split("/")[3].Replace('esr', '')
$script:HTTPFileName = $HTTPResponse.ResponseUri.LocalPath.Split("/") | Select-Object -Last 0
}
Function updateRepoFileInfos {
Write-Output "updateRepoFileInfos:`n"
#$script:RepoFileName = (Get-ChildItem -Path $WorkingDir *$extension | Sort-Object -Descending { [regex]::Replace($_.Name, '\d+', { $args[0].Value.PadLeft(20) }) } | Select-Object FullName, Name -First 1)
$script:RepoDirName = (Get-ChildItem -Path $WorkingDir -Directory | Sort-Object -Descending { [regex]::Replace($_.Name, '\d+', { $args[0].Value.PadLeft(20) }) } | Select-Object FullName, Name -First 1)
$script:RepoFileName = (Get-ChildItem -Path $RepoDirName.FullName -File -Recurse *$extension)
$script:RepoSize = (Get-ChildItem $RepoFileName.FullName).Length
$script:RepoVersion = $RepoFileName.Name.Replace('Firefox Setup ', '').Replace('esr', '').Replace(".$extension", '')
if(Test-Path -Path $lastDownload) {
$script:lastDownloadVersion = (Get-Content $lastDownload)
}
else {
$script:lastDownloadVersion = ""
}
}
Function DownloadRelease {
Write-Output "DownloadRelease started`n"
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest -Uri $HTTPFolderUrl -OutFile "$($RepoFileName.FullName)"
updateRepoFileInfos
Set-Content -Path $lastDownload -Value $RepoVersion
}
Function WhatToDo {
updateRepoFileInfos
if ($lastDownloadVersion -eq $RepoVersion) {
Write-Output "Versions seems to be the same: when did we last update this file?`n"
$lastWrite = (Get-Item $lastDownload ).LastWriteTime
$timeSpan = New-Timespan -days $waitDays -Hours 0 -Minutes 0
if (((Get-Date) - $lastWrite) -gt $timeSpan) {
Write-Output "Seems the file wasn't touched for at least $waitDays Days: let's Check the FileSizes`n"
getHTTPInfos
if ($HTTPSize -ne $RepoSize) {
Write-Output "FileSizes seem to be different: Let's Download a new File`n"
DownloadRelease
}
else {
Write-Output "FileSizes seem to be the same: Hooray!`n"
}
}
else { Write-Output "It seems the File was updated within the last $waitDays Days - I think we are done here: Hooray!`n"}
}
elseif($lastDownloadVersion -lt $RepoVersion) {
Write-Output "Version seems to be Newer: Let's Download a new File`n"
DownloadRelease
}
elseif($lastVersion -eq $null) {
Write-Output "Version seems to be ZERO? Let's Download a new File`n"
DownloadRelease
}
elseif(-not (Test-Path -Path $lastDownload)) {
Write-Output "Version File seems to be missing: Let's Download a new File`n"
DownloadRelease
}
else {
Write-Output "No rule did work: Looks GOOD!!!`n"
}
}
#Locking-Settings
$sleeptime = Get-Random -Min 5 -Maximum 12
$LockFile = $WorkingDir + $sep + "Execution.lock"
# Loop that runs until we have exclusive write access to $LockFile
while ($FileStream.CanWrite -eq $false) {
if (-not (Test-Path -Path $LockFile)) {
Set-Content -Path $LockFile -Value 'Lockfile'
}
try {
$FileStream = [System.IO.File]::Open($LockFile,'Open','Write')
}
catch {
Waiting $sleeptime seconds
Start-Sleep -Seconds $sleeptime
}
}
#No one else will do a File-Download while we are working! - Hopefully? o_0
WhatToDo
#We are finished!
$FileStream.Close
$FileStream.Dispose
Remove-Item -Path $LockFile -Force -ErrorAction Ignore
@crpb
Copy link
Author

crpb commented Jun 14, 2022

Natural sorting for all those Version-Jumps...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment