2
0

install-nsis.ps1 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #-----------------------------------------------------------------------------
  2. #
  3. # SRT - Secure, Reliable, Transport
  4. # Copyright (c) 2021, Thierry Lelegard
  5. #
  6. # This Source Code Form is subject to the terms of the Mozilla Public
  7. # License, v. 2.0. If a copy of the MPL was not distributed with this
  8. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. #
  10. #-----------------------------------------------------------------------------
  11. <#
  12. .SYNOPSIS
  13. Download, expand and install NSIS, the NullSoft Installer Scripting.
  14. .PARAMETER ForceDownload
  15. Force a download even if NSIS is already downloaded.
  16. .PARAMETER NoInstall
  17. Do not install the NSIS package. By default, NSIS is installed.
  18. .PARAMETER NoPause
  19. Do not wait for the user to press <enter> at end of execution. By default,
  20. execute a "pause" instruction at the end of execution, which is useful
  21. when the script was run from Windows Explorer.
  22. #>
  23. [CmdletBinding(SupportsShouldProcess=$true)]
  24. param(
  25. [switch]$ForceDownload = $false,
  26. [switch]$NoInstall = $false,
  27. [switch]$NoPause = $false
  28. )
  29. Write-Output "NSIS download and installation procedure"
  30. $NSISPage = "https://nsis.sourceforge.io/Download"
  31. $FallbackURL = "http://prdownloads.sourceforge.net/nsis/nsis-3.05-setup.exe?download"
  32. # A function to exit this script.
  33. function Exit-Script([string]$Message = "")
  34. {
  35. $Code = 0
  36. if ($Message -ne "") {
  37. Write-Output "ERROR: $Message"
  38. $Code = 1
  39. }
  40. if (-not $NoPause) {
  41. pause
  42. }
  43. exit $Code
  44. }
  45. # Local file names.
  46. $RootDir = $PSScriptRoot
  47. $TmpDir = "$RootDir\tmp"
  48. # Create the directory for external products when necessary.
  49. [void] (New-Item -Path $TmpDir -ItemType Directory -Force)
  50. # Without this, Invoke-WebRequest is awfully slow.
  51. $ProgressPreference = 'SilentlyContinue'
  52. # Get the HTML page for NSIS downloads.
  53. $status = 0
  54. $message = ""
  55. $Ref = $null
  56. try {
  57. $response = Invoke-WebRequest -UseBasicParsing -UserAgent Download -Uri $NSISPage
  58. $status = [int] [Math]::Floor($response.StatusCode / 100)
  59. }
  60. catch {
  61. $message = $_.Exception.Message
  62. }
  63. if ($status -ne 1 -and $status -ne 2) {
  64. # Error fetch NSIS download page.
  65. if ($message -eq "" -and (Test-Path variable:response)) {
  66. Write-Output "Status code $($response.StatusCode), $($response.StatusDescription)"
  67. }
  68. else {
  69. Write-Output "#### Error accessing ${NSISPage}: $message"
  70. }
  71. }
  72. else {
  73. # Parse HTML page to locate the latest installer.
  74. $Ref = $response.Links.href | Where-Object { $_ -like "*/nsis-*-setup.exe?download" } | Select-Object -First 1
  75. }
  76. if (-not $Ref) {
  77. # Could not find a reference to NSIS installer.
  78. $Url = [System.Uri]$FallbackURL
  79. }
  80. else {
  81. # Build the absolute URL's from base URL (the download page) and href links.
  82. $Url = New-Object -TypeName 'System.Uri' -ArgumentList ([System.Uri]$NSISPage, $Ref)
  83. }
  84. $InstallerName = (Split-Path -Leaf $Url.LocalPath)
  85. $InstallerPath = "$TmpDir\$InstallerName"
  86. # Download installer
  87. if (-not $ForceDownload -and (Test-Path $InstallerPath)) {
  88. Write-Output "$InstallerName already downloaded, use -ForceDownload to download again"
  89. }
  90. else {
  91. Write-Output "Downloading $Url ..."
  92. Invoke-WebRequest -UseBasicParsing -UserAgent Download -Uri $Url -OutFile $InstallerPath
  93. if (-not (Test-Path $InstallerPath)) {
  94. Exit-Script "$Url download failed"
  95. }
  96. }
  97. # Install NSIS
  98. if (-not $NoInstall) {
  99. Write-Output "Installing $InstallerName"
  100. Start-Process -FilePath $InstallerPath -ArgumentList @("/S") -Wait
  101. }
  102. Exit-Script