Install-SecureMFA_RDG_WebPortal.ps1

function Install-SecureMFA_RDG_WebPortal {

<#
     .DESCRIPTION
    Deploys SecureMFA RD Gateway Portal. It is a single HTML file which allows users to generate RDP file which allows to initiated RDP connection with OTP and RD Gateway server parameters.
  
    .PARAMETER GatewayHostname
    - GatewayHostname [Optional] � Sets RD Gateway default parameter for a website .
 
    .PARAMETER RDPortalPath
    - RDPortalPath [Optional] � Allows to change default website deployment path. Default path is 'C:\inetpub\wwwroot'
 
    .EXAMPLE
    # Deploys SecureMFA RD Gateway Portal into default location.
    Install-SecureMFA_RD_Gateway_Portal -GatewayHostname rdgateway.adatum.labnet
 
    .LINK
    https://www.securemfa.com/downloads/mfa-rds-otp
 
 
#>


[CmdletBinding()]
Param(
        [Parameter(Mandatory=$false)][ValidateNotNullOrEmpty()][string]$GatewayHostname,      
        [Parameter(Mandatory=$false)][ValidateNotNullOrEmpty()][string]$RDPortalPath = 'C:\inetpub\wwwroot'
)    
        

#HTML file Template
$htmlfile = @'
<!doctype html>
 
<html lang="en">
<head>
  <meta charset="utf-8">
 
  <title>SecureMFA RD Gateway Authentication Portal</title>
  <meta name="description" content="SecureMFA RD Gateway OTP Authentication Portal">
  <meta name="author" content="SecureMFA">
 
</head>
 
<body>
<style>
body {
  background-image: url('background.jpg');
  background-repeat: no-repeat;
  background-attachment: fixed;
  background-size: 100% 100%;
}
</style>
<center>
  <div style="background-color:white; width: 90%;" >
  <br /><b><label for="info">SecureMFA RD Gateway OTP Authentication Portal</label></B><br /><br />
  <textarea id="server" rows="1" placeholder="Server" autofocus cols="50"></textarea><br/>
  <textarea id="user" rows="1" placeholder="DOMAIN\USER" cols="50"></textarea><br/>
  <textarea id="otp" rows="1" placeholder="OTP" maxlength="6" cols="50"></textarea><br/>
  <textarea id="gatewayhostname" rows="1" placeholder="RD Gateway Server" cols="50">RDGatewayServerText</textarea><br/>
  <br /><input type="button" id="rdpconnect-btn" value="RDP Connect"/><br /><br />
  </div>
</center>
  <script>
    function download(filename, text) {
        var element = document.createElement('a');
        element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
        element.setAttribute('download', filename);
 
        element.style.display = 'none';
        document.body.appendChild(element);
 
        element.click();
 
        document.body.removeChild(element);
    }
 
    // Start file download.
    document.getElementById("rdpconnect-btn").addEventListener("click", function(){
    // Generate download of hello.txt file with some content
    var otpvalue = document.getElementById("otp").value;
    var filename = document.getElementById("server").value + ".rdp";
    var anchordomain = "securemfa.com";
 
    var RDPSettings = [
        'screen mode id:i:2',
        'use multimon:i:0',
        'desktopwidth:i:1920',
        'desktopheight:i:1200',
        'session bpp:i:32',
        'winposstr:s:0,3,0,0,800,600',
        'compression:i:1',
        'keyboardhook:i:2',
        'audiocapturemode:i:0',
        'videoplaybackmode:i:1',
        'connection type:i:7',
        'networkautodetect:i:1',
        'bandwidthautodetect:i:1',
        'displayconnectionbar:i:1',
        'enableworkspacereconnect:i:0',
        'disable wallpaper:i:0',
        'allow font smoothing:i:0',
        'allow desktop composition:i:0',
        'disable full window drag:i:1',
        'disable menu anims:i:1',
        'disable themes:i:0',
        'disable cursor setting:i:0',
        'bitmapcachepersistenable:i:1',
        'audiomode:i:0',
        'redirectprinters:i:1',
        'redirectcomports:i:0',
        'redirectsmartcards:i:1',
        'redirectclipboard:i:1',
        'redirectposdevices:i:0',
        'autoreconnection enabled:i:1',
        'authentication level:i:2',
        'prompt for credentials:i:0',
        'negotiate security layer:i:1',
        'remoteapplicationmode:i:0',
        'alternate shell:s:',
        'shell working directory:s:',
        'gatewayusagemethod:i:1',
        'gatewayprofileusagemethod:i:1',
        'promptcredentialonce:i:0',
        'gatewaybrokeringtype:i:0',
        'use redirection server name:i:0',
        'rdgiskdcproxy:i:0',
        'kdcproxyname:s:',
        'gatewaycredentialssource:i:5',
        ('gatewayhostname:s:' + document.getElementById("gatewayhostname").value),
        ('gatewayaccesstoken:s:' + document.getElementById("user").value + ":" + document.getElementById("otp").value + ":" + anchordomain),
        ('full address:s:' + document.getElementById("server").value),
        ''].join('\n');
     
     
        download(filename, RDPSettings.replace(/\n/g, '\r\n'));
    }, false);
  </script>
   
   
</body>
</html>
'@


#Replace values with static params
$htmlfile = $htmlfile.Replace('AnchorDomainText', $AnchorDomain)
$htmlfile = $htmlfile.Replace('RDGatewayServerText', $GatewayHostname)

    Try {    
            
        $deploymentpath = Join-Path $RDPortalPath "index.html"
       
        if(Test-Path $RDPortalPath) {
        $htmlfile | Out-File $deploymentpath -Force
        write-host "SecureMFA RD Gateway Portal has been deployed $deploymentpath" -ForegroundColor Cyan
        }
        else {write-host "$RDPortalPath doesn't exist. Please choose a valid path." -ForegroundColor Red}


    } Catch {Write-host $_.Exception.message -ForegroundColor Red}
}