Being pedantic

This commit is contained in:
Adrien Ferrand 2019-11-12 15:21:23 +01:00
parent 8f1380a90a
commit b2e7219411

View file

@ -1,81 +1,85 @@
#Requires -RunAsAdministrator
Param(
[Parameter(Mandatory=$true)]
[string]$InstallDir
[CmdletBinding()]
param(
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
[string]
$InstallDir
)
begin {}
process {
Start-Transcript -Path "C:\Certbot\log\auto-update.log"
trap {
Stop-Transcript
}
Start-Transcript -Path "C:\Certbot\log\auto-update.log"
trap {
Stop-Transcript
}
$ErrorActionPreference = 'Stop'
$ErrorActionPreference = 'Stop'
$installerAuthenticodeCertificateThumbprint = "CHANGEME"
$installerAuthenticodeCertificateThumbprint = "CHANGEME"
# Get current local certbot version
try {
$currentVersion = certbot --version
$currentVersion = $currentVersion -replace '^certbot (\d+\.\d+\.\d+).*$', '$1'
} catch {
"An error occured while fetching the current local certbot version:"
$_.Exception
"Assuming Certbot is not up-to-date."
$currentVersion = "0.0.0"
}
# Get current local certbot version
try {
$currentVersion = certbot --version
$currentVersion = $currentVersion -replace '^certbot (\d+\.\d+\.\d+).*$', '$1'
} catch {
"An error occured while fetching the current local certbot version:"
$_.Exception
"Assuming Certbot is not up-to-date."
$currentVersion = "0.0.0"
}
# Get latest remote certbot version
try {
$result = Invoke-RestMethod -Uri https://api.github.com/repos/certbot/certbot/releases/latest
$latestVersion = $result.tag_name -replace '^v(\d+\.\d+\.\d+).*$', '$1'
} catch {
"Could not get the latest remote certbot version. Error was:"
$_.Exception
throw "Aborting auto-upgrade process."
}
# Get latest remote certbot version
try {
$result = Invoke-RestMethod -Uri https://api.github.com/repos/certbot/certbot/releases/latest
$latestVersion = $result.tag_name -replace '^v(\d+\.\d+\.\d+).*$', '$1'
} catch {
"Could not get the latest remote certbot version. Error was:"
$_.Exception
throw "Aborting auto-upgrade process."
}
if ([System.Version]"$currentVersion" -ge [System.Version]"$latestVersion") {
"No upgrade is needed, Certbot is already at the latest version ($currentVersion)."
} else {
# Search for the Windows installer asset
$installerUrl = $null
foreach ($asset in $result.assets) {
if ($asset.name -match '^certbot-.*installer-win32\.exe$') {
$installerUrl = $asset.browser_download_url
}
}
if ([System.Version]"$currentVersion" -ge [System.Version]"$latestVersion") {
"No upgrade is needed, Certbot is already at the latest version ($currentVersion)."
} else {
# Search for the Windows installer asset
$installerUrl = $null
foreach ($asset in $result.assets) {
if ($asset.name -match '^certbot-.*installer-win32\.exe$') {
$installerUrl = $asset.browser_download_url
if ($null -eq $installerUrl) {
throw "Could not find the URL for the latest Certbot for Windows installer."
}
"Starting Certbot auto-upgrade from $currentVersion to $latestVersion ..."
$installerPath = "$env:TMP/certbot-installer-win32.exe"
try {
# Download the installer
"Downloading the installer ..."
$webClient = New-Object System.Net.WebClient
$webClient.DownloadFile($installerUrl, $installerPath)
# Check installer has a valid signature from the Certbot release team
$signature = Get-AuthenticodeSignature $installerPath
# Uncomment the following lines of code once the Certbot installer is correctly signed.
# if ($signature.Status -ne 'Valid') {
# throw "Downloaded installer has no or invalid Authenticode signature."
# }
# if ($signature.SignerCertificate.Thumbprint -ne $installerAuthenticodeCertificateThumbprint) {
# throw "Downloaded installer has not been signed by Certbot development team."
# }
# Install new version of Certbot
"Running the installer ..."
Start-Process -FilePath $installerPath -ArgumentList "/S /D=$InstallDir"
"Certbot $latestVersion is installed."
} finally {
Remove-Item $installerPath -ErrorAction 'Ignore'
}
}
if ($null -eq $installerUrl) {
throw "Could not find the URL for the latest Certbot for Windows installer."
}
"Starting Certbot auto-upgrade from $currentVersion to $latestVersion ..."
$installerPath = "$env:TMP/certbot-installer-win32.exe"
try {
# Download the installer
"Downloading the installer ..."
$webClient = New-Object System.Net.WebClient
$webClient.DownloadFile($installerUrl, $installerPath)
# Check installer has a valid signature from the Certbot release team
$signature = Get-AuthenticodeSignature $installerPath
# Uncomment the following lines of code once the Certbot installer is correctly signed.
# if ($signature.Status -ne 'Valid') {
# throw "Downloaded installer has no or invalid Authenticode signature."
# }
# if ($signature.SignerCertificate.Thumbprint -ne $installerAuthenticodeCertificateThumbprint) {
# throw "Downloaded installer has not been signed by Certbot development team."
# }
# Install new version of Certbot
"Running the installer ..."
Start-Process -FilePath $installerPath -ArgumentList "/S /D=$InstallDir"
"Certbot $latestVersion is installed."
} finally {
Remove-Item $installerPath -ErrorAction 'Ignore'
}
}
end {}