install-openssl.ps1 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 OpenSSL for Windows.
  14. .PARAMETER ForceDownload
  15. Force a download even if the OpenSSL installers are already downloaded.
  16. .PARAMETER NoInstall
  17. Do not install the OpenSSL packages. By default, OpenSSL 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 "OpenSSL download and installation procedure"
  30. $OpenSSLHomePage = "http://slproweb.com/products/Win32OpenSSL.html"
  31. # A function to exit this script.
  32. function Exit-Script([string]$Message = "")
  33. {
  34. $Code = 0
  35. if ($Message -ne "") {
  36. Write-Output "ERROR: $Message"
  37. $Code = 1
  38. }
  39. if (-not $NoPause) {
  40. pause
  41. }
  42. exit $Code
  43. }
  44. # Local file names.
  45. $RootDir = $PSScriptRoot
  46. $TmpDir = "$RootDir\tmp"
  47. # Create the directory for external products when necessary.
  48. [void] (New-Item -Path $TmpDir -ItemType Directory -Force)
  49. # Without this, Invoke-WebRequest is awfully slow.
  50. $ProgressPreference = 'SilentlyContinue'
  51. # Get the HTML page for OpenSSL downloads.
  52. $status = 0
  53. $message = ""
  54. try {
  55. $response = Invoke-WebRequest -UseBasicParsing -UserAgent Download -Uri $OpenSSLHomePage
  56. $status = [int] [Math]::Floor($response.StatusCode / 100)
  57. }
  58. catch {
  59. $message = $_.Exception.Message
  60. }
  61. if ($status -ne 1 -and $status -ne 2) {
  62. if ($message -eq "" -and (Test-Path variable:response)) {
  63. Exit-Script "Status code $($response.StatusCode), $($response.StatusDescription)"
  64. }
  65. else {
  66. Exit-Script "#### Error accessing ${OpenSSLHomePage}: $message"
  67. }
  68. }
  69. # Parse HTML page to locate the latest MSI files.
  70. $Ref32 = $response.Links.href | Where-Object { $_ -like "*/Win32OpenSSL-*.msi" } | Select-Object -First 1
  71. $Ref64 = $response.Links.href | Where-Object { $_ -like "*/Win64OpenSSL-*.msi" } | Select-Object -First 1
  72. # Build the absolute URL's from base URL (the download page) and href links.
  73. $Url32 = New-Object -TypeName 'System.Uri' -ArgumentList ([System.Uri]$OpenSSLHomePage, $Ref32)
  74. $Url64 = New-Object -TypeName 'System.Uri' -ArgumentList ([System.Uri]$OpenSSLHomePage, $Ref64)
  75. # Download and install one MSI package.
  76. function Download-Install([string]$Url)
  77. {
  78. $MsiName = (Split-Path -Leaf $Url.toString())
  79. $MsiPath = "$TmpDir\$MsiName"
  80. if (-not $ForceDownload -and (Test-Path $MsiPath)) {
  81. Write-Output "$MsiName already downloaded, use -ForceDownload to download again"
  82. }
  83. else {
  84. Write-Output "Downloading $Url ..."
  85. Invoke-WebRequest -UseBasicParsing -UserAgent Download -Uri $Url -OutFile $MsiPath
  86. }
  87. if (-not (Test-Path $MsiPath)) {
  88. Exit-Script "$Url download failed"
  89. }
  90. if (-not $NoInstall) {
  91. Write-Output "Installing $MsiName"
  92. Start-Process msiexec.exe -ArgumentList @("/i", $MsiPath, "/qn", "/norestart") -Wait
  93. }
  94. }
  95. # Download and install the two MSI packages.
  96. Download-Install $Url32
  97. Download-Install $Url64
  98. Exit-Script