powershell-scripts/windows/Install-WinAcme.ps1

66 lines
2.3 KiB
PowerShell

[CmdletBinding()]
param (
[Parameter()]
[String]
$DestinationPath = 'C:\Program Files\win-acme'
)
<#
.SYNOPSIS
Install win-acme
.DESCRIPTION
This script downloads and installs the latest version of win-acme.
.PARAMETER DestinationPath
Specifies the path into which win-acme is installed
.LINK
https://www.win-acme.com/
#>
begin {
# Prep
$Releases = (Invoke-WebRequest 'https://api.github.com/repos/win-acme/win-acme/releases' -UseBasicParsing).Content | ConvertFrom-Json | Where-Object { $_.prerelease -eq $false -and $_.draft -eq $false }
$ReleaseTag = ($Releases | Select-Object -First 1).tag_name
# $InstallerUrl = 'https://github.com/win-acme/win-acme/releases/download/v2.2.9.1701/win-acme.v2.2.9.1701.x64.pluggable.zip'
$InstallerUrl = "https://github.com/win-acme/win-acme/releases/download/$ReleaseTag/win-acme.$ReleaseTag.x64.pluggable.zip"
$ArchivePath = Join-Path -Path $env:TEMP -ChildPath ($InstallerUrl | Select-String -Pattern "(win-acme\.v?(?:\d\.?)+\.x64\.pluggable\.zip)").Matches.Value
# Exit if the application is already installed
if (Test-Path $DestinationPath) { Write-Host 'win-acme is already installed.'; exit }
}
process {
# Download
try {
Write-Host "Downloading application..."
# Use Legacy WebClient for compatibility
$ProgressPreference = 'SilentlyContinue'
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls12
(New-Object Net.WebClient).DownloadFile($InstallerUrl, $ArchivePath)
} catch {
throw "Failed downloading application. $_"
}
# Install
try {
Write-Host "Installing application..."
Expand-Archive -Path $ArchivePath -DestinationPath $DestinationPath -Force
# Add the DestinationPath to Path system environment variable
if ($DestinationPath -notin $env:PATH) { $env:PATH = $env:PATH + ";$DestinationPath\"; [Environment]::SetEnvironmentVariable("Path", $env:PATH, [System.EnvironmentVariableTarget]::Machine) }
} catch {
throw "Failed installing application. $_"
}
}
end {
# Clean up
if (-not (Test-Path -Path $ArchivePath)) {
Write-Host "Removing package download from '$ArchivePath'"
Remove-Item -Path $ArchivePath -Force
}
}