Public/Get-WinOpenSSL.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<#
    .SYNOPSIS
        This function downloads openssl.exe from either https://indy.fulgan.com/SSL/ or
        http://wiki.overbyte.eu/wiki/index.php/ICS_Download" and adds it to $env:Path
 
    .DESCRIPTION
        See .SYNOPSIS
 
    .PARAMETER OpenSSLWinBinariesUrl
        This parameter is OPTIONAL, however, it has a default value of https://indy.fulgan.com/SSL/
 
        This parameter takes a string that represents the Url that contains a link to a zip file
        containing openssl.exe.
 
    .EXAMPLE
        # Open an elevated PowerShell Session, import the module, and -
 
        PS C:\Users\zeroadmin> Get-WinOpenSSL
         
#>

function Get-WinOpenSSL {
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory=$False)]
        [ValidateSet("https://indy.fulgan.com/SSL/","http://wiki.overbyte.eu/wiki/index.php/ICS_Download")]
        [string]$OpenSSLWinBinariesUrl = "https://indy.fulgan.com/SSL/"
    )

    if ($PSVersionTable.PSEdition -eq "Desktop") {
        [Net.ServicePointManager]::SecurityProtocol = "tls12, tls11, tls"
    }

    $IWRResult = Invoke-WebRequest -Uri $OpenSSLWinBinariesUrl -UseBasicParsing

    if ($OpenSSLWinBinariesUrl -match "fulgan") {
        $LatestOpenSSLWinBinaryUrl = $OpenSSLWinBinariesUrl + $($IWRResult.Links | Where-Object {$_.OuterHTML -match "win64\.zip"})[-1].href
    }
    if ($OpenSSLWinBinariesUrl -match "overbyte") {
        $LatestOpenSSLWinBinaryUrl = $($IWRResult.Links | Where-Object {$_.OuterHTML -match "win64\.zip"})[0].href
    }
    $OutputFileName = $($LatestOpenSSLWinBinaryUrl -split '/')[-1]
    $OutputFilePath = "$HOME\Downloads\$OutputFileName"

    Invoke-WebRequest -Uri $LatestOpenSSLWinBinaryUrl -OutFile $OutputFilePath

    $ExpansionDirectory = $OutputFilePath -replace '\.zip$',''
    if (Test-Path $ExpansionDirectory) {
        Remove-Item "$ExpansionDirectory\*" -Recurse -Force
    }
    else {
        $null = New-Item -ItemType Directory -Path $ExpansionDirectory
    }
    $null = Expand-Archive -Path $OutputFilePath -DestinationPath $ExpansionDirectory -Force
    $WinOpenSSLFiles = Get-ChildItem -Path $ExpansionDirectory

    $WinOpenSSLParentDir = $WinOpenSSLFiles[0].Directory.FullName
    [System.Collections.Arraylist][array]$CurrentEnvPathArray = $env:Path -split ';' | Where-Object {![System.String]::IsNullOrWhiteSpace($_)}
    if ($CurrentEnvPathArray -notcontains $WinOpenSSLParentDir) {
        $CurrentEnvPathArray.Insert(0,$WinOpenSSLParentDir)
        $env:Path = $CurrentEnvPathArray -join ';'
    }
}