Install-SecureMFA_DEV_Environment.ps1

#Requires -RunAsAdministrator

<#
     .SYNOPSIS
        Installs SecureMFA DEV Environment.
    .DESCRIPTION
        Installs DEV Environment for quick start.
 
        Dependencies:
            * System must be connected to the Internet to download dependancies.
  
    .NOTES
        Version: 1.0.1.4
        Author: SecureMfa.com
        Creation Date: 21/03/2021
        Purpose/Change:
   
    .EXAMPLE
        C:\PS> Install-SecureMFA_DEV_Environment -Env JAVA
 
        This command will install JAVA DEV Environment.
    
#>



#Check if windows events source for application log exist, if not create one.
if ([System.Diagnostics.EventLog]::SourceExists("SecureMFA_SupportTools") -eq $False) {New-EventLog -LogName "Application" -Source "SecureMFA_SupportTools" ; Write-Host "SecureMFA_SupportTools Log Source Created."}

Function Install-SecureMFA_MSI($msi_path)
    {
        write-host "Installing $msi_path ..." -ForegroundColor Yellow
        $file = get-item $msi_path
        $DataStamp = get-date -Format yyyyMMddTHHmmss
        $logFile = '{0}-{1}.log' -f $file.fullname,$DataStamp
        $MSIArguments = @(
            "/i"
            ('"{0}"' -f $file.fullname)
            "/qn"
            "/norestart"
            "/L*v"
            $logFile
        )
        Start-Process "msiexec.exe" -ArgumentList $MSIArguments -Wait -NoNewWindow
        write-host "Installing $msi_path Success." -ForegroundColor Green
    }     

Function Install-SecureMFA_DEV_Environment {
Param
(
    [Parameter(Mandatory=$false)][ValidateSet('JAVA','None')][string]$Env='JAVA'
)
    
    #Static Variables
    $AppsPath = "c:\Apps"
    $DistributionSource = $null; 
    $DistributionSite = $null;
    
    #Free distributions of OpenJDK 11 (hotspot) or later
    $openjdk_uri = 'https://github.com/AdoptOpenJDK/openjdk11-binaries/releases/download/jdk-11.0.10%2B9/OpenJDK11U-jdk_x64_windows_hotspot_11.0.10_9.msi';
    $openjdk_name = 'OpenJDK11U-jdk_x64_windows_hotspot_11.0.10_9.msi'
    $openjdk_site = 'https://adoptopenjdk.net/'
    $openjdk_path = ($AppsPath + "\" + $openjdk_name) 
    $openjdk_testpath = 'HKLM:\SOFTWARE\AdoptOpenJDK\JDK\11.0.10.9'
    #Downloading Apache Maven 3.6.3 Binary zip archive or later
    $maven_uri = 'https://mirrors.ukfast.co.uk/sites/ftp.apache.org/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.zip';
    $maven_name = 'apache-maven-3.6.3-bin.zip'
    $maven_site = 'https://maven.apache.org/download.cgi'
    $maven_path = ($AppsPath + "\" + $maven_name)
    $maven_testpath = ($AppsPath + "\apache-maven")
    #OpenSSL
    $openssl_x64_uri = 'https://slproweb.com/download/Win64OpenSSL_Light-1_1_1j.msi' 
    $openssl_x32_uri = 'https://slproweb.com/download/Win32OpenSSL_Light-1_1_1j.msi'
    $openssl_name = 'OpenSSL_Light-1_1_1j.msi'
    $openssl_site = 'https://slproweb.com/products/Win32OpenSSL.html'
    $openssl_path = ($AppsPath + "\" + $openssl_name)
    $openssl_testpath = $null
  
    try
    {
        $Error.Clear() 

        if($Env -eq 'JAVA') {

            write-host "Installing SecureMFA $Env DEV Environment" -ForegroundColor Cyan

            #Create Apps folder
            If(!(test-path $AppsPath)) {New-Item -ItemType Directory -Force -Path $AppsPath}

            #OpenJDK distribution
            $DistributionSource = $openjdk_uri; 
            $DistributionSite = $openjdk_site;    
            If(test-path $openjdk_path) {write-host "OpenJDK distribution $openjdk_path exist - skipping... " -ForegroundColor Yellow} else {write-host "OpenJDK distribution $openjdk_path download started... " -ForegroundColor Green; Invoke-WebRequest -Uri $openjdk_uri -OutFile $openjdk_path}

            #Apache Maven distribution
            $DistributionSource = $maven_uri; 
            $DistributionSite = $maven_site;
            If(test-path $maven_path) {write-host "Apache Maven distribution $maven_path exist - skipping... " -ForegroundColor Yellow} else {write-host "Apache Maven distribution $maven_path download started... " -ForegroundColor Green; Invoke-WebRequest -Uri $maven_uri -OutFile $maven_path}
        
            #OpenSSL distribution
            $openssl_uri; 
            #OS Architecture 32bit or 64bit
            if([System.IntPtr]::Size -eq 4) {$openssl_uri = $openssl_x32_uri; $openssl_testpath = ($env:ProgramFiles + '\OpenSSL-Win32')} else { $openssl_uri = $openssl_x64_uri; $openssl_testpath = ($env:ProgramFiles + '\OpenSSL-Win64')}     
            $DistributionSource = $openssl_uri ; 
            $DistributionSite = $openssl_site;
        
            If(test-path $openssl_path) {write-host "OpenSSL distribution $openssl_path exist - skipping... " -ForegroundColor Yellow} else {write-host "OpenSSL distribution $openssl_path download started... " -ForegroundColor Green; Invoke-WebRequest -Uri $openssl_uri -OutFile $openssl_path}
        
            $DistributionSource = $null; 
            $DistributionSite = $null;

            #Installation OpenSSL
            $message  = "Do you want to reinstall existing OpenSSL " + $openssl_name + " ?";            
            $question = 'Please confirm?'
            $choices = New-Object Collections.ObjectModel.Collection[Management.Automation.Host.ChoiceDescription]
            $choices.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList '&Yes'))
            $choices.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList '&No'))
            #Found existing installation
            if(Test-Path $openssl_testpath) {   
            $decision_Validation = $Host.UI.PromptForChoice($message, $question, $choices, 0)
            if ($decision_Validation -eq 1 ) {Write-Host "OpenSSL installation has been cancelled, skipping!" -ForegroundColor Yellow ;} 
            else {
                    Install-SecureMFA_MSI($openssl_path)
                 }
            }
            #No existing installation
            else {
                    Install-SecureMFA_MSI($openssl_path)
                 }

            #Installation OpenJDK
            $message  = "Do you want to reinstall existing OpenJDK " + $openjdk_name + " ?";            
            $question = 'Please confirm?'
            $choices = New-Object Collections.ObjectModel.Collection[Management.Automation.Host.ChoiceDescription]
            $choices.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList '&Yes'))
            $choices.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList '&No'))
            #Found existing installation
            if(Test-Path $openjdk_testpath) {   
            $decision_Validation = $Host.UI.PromptForChoice($message, $question, $choices, 0)
            if ($decision_Validation -eq 1 ) {Write-Host "OpenJDK installation has been cancelled, skipping!" -ForegroundColor Yellow ;} 
            else {
                    Install-SecureMFA_MSI($openjdk_path)
                    $reg = "Registry::HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
                    $path = (Get-ItemProperty -Path "$reg" -Name PATH).Path
                    $pos = $path.IndexOf(";")
                    $JDKfullpath = $path.Substring(0, $pos)
                    $JDKpath = $JDKfullpath -replace ".{4}$"
                    setx -m JAVA_HOME $JDKpath
                 }
            }
            #No existing installation
            else {
                    Install-SecureMFA_MSI($openjdk_path)
                    $reg = "Registry::HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
                    $path = (Get-ItemProperty -Path "$reg" -Name PATH).Path
                    $pos = $path.IndexOf(";")
                    $JDKfullpath = $path.Substring(0, $pos)
                    $JDKpath = $JDKfullpath -replace ".{4}$"
                    setx -m JAVA_HOME $JDKpath
                 }

            #Installation Apache Maven
            $message  = "Do you want to reinstall existing Apache Maven " + $maven_name + " ?";            
            $question = 'Please confirm?'
            $choices = New-Object Collections.ObjectModel.Collection[Management.Automation.Host.ChoiceDescription]
            $choices.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList '&Yes'))
            $choices.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList '&No'))
            #Found existing installation
            if(Test-Path $maven_testpath) {   
            $decision_Validation = $Host.UI.PromptForChoice($message, $question, $choices, 0)
            if ($decision_Validation -eq 1 ) {Write-Host "OpenJDK installation has been cancelled, skipping!" -ForegroundColor Yellow ;} 
            else {
                    Remove-Item -Recurse -Force $maven_testpath
                    Expand-Archive -LiteralPath $maven_path -DestinationPath $maven_testpath -Force
                    $MVNpath = Get-ChildItem -Path $maven_testpath -Directory -Force -ErrorAction SilentlyContinue | select FullName
                    setx -m MAVEN_HOME $MVNpath.FullName
                 }
            }
            #No existing installation
            else {
                    Expand-Archive -LiteralPath $maven_path -DestinationPath $maven_testpath -Force
                    $MVNpath = Get-ChildItem -Path $maven_testpath -Directory -Force -ErrorAction SilentlyContinue | select FullName
                    setx -m MAVEN_HOME $MVNpath.FullName
                 }
        
            #Set Path variables
            SETX PATH "%PATH%;%MAVEN_HOME%\bin;%OpenSSL_HOME%\bin;"        
            write-host "SecureMFA $Env DEV Environment installation completed." -ForegroundColor Cyan
        }

        else {write-host "No DEV Environment selected for deployment." -ForegroundColor Cyan}
    }
    catch
    {
        if(!([string]::IsNullOrEmpty($DistributionSource))) { Write-Host "Failed to download $DistributionSource , please update source location from $DistributionSite and try again." -ForegroundColor red }
        Write-Host "$($MyInvocation.InvocationName): $_" -ForegroundColor red 
        
    }

}