Install-SecureMFA_COM_Extensions.ps1

#Requires -RunAsAdministrator

<#
     .SYNOPSIS
        Installs COM extensions for SecureMFA.com provider.
    .DESCRIPTION
        SecureMFA_SupportTools.dll COM extensions are used by Microsoft RDS Gateway server to provide OTP functionality.
        COM extensions are only required to be present on RDS gateway server when SecureMFA_RDS provider is used to enable OTP codes for this service.
 
        Dependencies:
            * System which executes a script must have Microsoft Framework 4.6.1 and above installed.
            * SecureMFA_SupportTools.dll file must be present in script directory.
  
    .NOTES
        Version: $dllversion
        Author: SecureMfa.com
        Creation Date: 03/12/2019
        Purpose/Change: Added COM extensions
   
    .EXAMPLE
        C:\PS> Install-SecureMFA_COM_Extensions
 
        This command will install SecureMFA_SupportTools.dll COM extensions on the server.
    
#>


$dllpath = (Join-Path -Path $PSScriptRoot -ChildPath SecureMFA_SupportTools.dll)
$dllversion = [System.Diagnostics.FileVersionInfo]::GetVersionInfo("$dllpath").FileVersion
Write-Host "File: $dllpath Version: $dllversion"

#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."}

#Load GAC Assembly
Set-location $PSScriptRoot            
[System.Reflection.Assembly]::Load("System.EnterpriseServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a") 
$publish = New-Object System.EnterpriseServices.Internal.Publish

Function Install-SecureMFA_COM_Extensions {
Param
(
    [Parameter(Mandatory=$false, ParameterSetName="Default")]
    [Switch]$Force
)
    
    #Check if TSGateway existi on the system
    if(((Get-Service tsgateway -ErrorAction SilentlyContinue).Status -eq $null) -and (!($Force))) {write-host "COM extensions are only required for RDS Gateway when used with SecureMFA_RDS_OTP provider. TS Gateway services doesn't exist on $env:COMPUTERNAME" -ForegroundColor Yellow; break}
    
    try
    {
        $Error.Clear()
        if (!(Test-Path $dllpath -Type Leaf) ) { throw "The assembly $dllpath does not exist" }

        write-host "Installing SecureMFA_SupportTools on the server" -ForegroundColor Cyan

        #Remove SecureMfaOtpProvider DLL from GAC assembly
        $publish.GacRemove($dllpath)       

        #Add SecureMfaOtpProvider DLL to GAC assembly
        $publish.GacInstall($dllpath)     

        #Add COM registry entries
        if((Test-Path -LiteralPath "HKLM:\SOFTWARE\Classes\SecureMFA_SupportTools.SecureMFACOM_Class") -ne $true) {  New-Item "HKLM:\SOFTWARE\Classes\SecureMFA_SupportTools.SecureMFACOM_Class" -force -ea SilentlyContinue };
        if((Test-Path -LiteralPath "HKLM:\SOFTWARE\Classes\SecureMFA_SupportTools.SecureMFACOM_Class\CLSID") -ne $true) {  New-Item "HKLM:\SOFTWARE\Classes\SecureMFA_SupportTools.SecureMFACOM_Class\CLSID" -force -ea SilentlyContinue };
        if((Test-Path -LiteralPath "HKLM:\SOFTWARE\Classes\CLSID\{70A8A539-0204-4DB6-B52A-3B467A7F4134}") -ne $true) {  New-Item "HKLM:\SOFTWARE\Classes\CLSID\{70A8A539-0204-4DB6-B52A-3B467A7F4134}" -force -ea SilentlyContinue };
        if((Test-Path -LiteralPath "HKLM:\SOFTWARE\Classes\CLSID\{70A8A539-0204-4DB6-B52A-3B467A7F4134}\InprocServer32") -ne $true) {  New-Item "HKLM:\SOFTWARE\Classes\CLSID\{70A8A539-0204-4DB6-B52A-3B467A7F4134}\InprocServer32" -force -ea SilentlyContinue };
        if((Test-Path -LiteralPath "HKLM:\SOFTWARE\Classes\CLSID\{70A8A539-0204-4DB6-B52A-3B467A7F4134}\InprocServer32\$dllversion") -ne $true) {  New-Item "HKLM:\SOFTWARE\Classes\CLSID\{70A8A539-0204-4DB6-B52A-3B467A7F4134}\InprocServer32\$dllversion" -force -ea SilentlyContinue };
        if((Test-Path -LiteralPath "HKLM:\SOFTWARE\Classes\CLSID\{70A8A539-0204-4DB6-B52A-3B467A7F4134}\ProgId") -ne $true) {  New-Item "HKLM:\SOFTWARE\Classes\CLSID\{70A8A539-0204-4DB6-B52A-3B467A7F4134}\ProgId" -force -ea SilentlyContinue };
        if((Test-Path -LiteralPath "HKLM:\SOFTWARE\Classes\CLSID\{70A8A539-0204-4DB6-B52A-3B467A7F4134}\Implemented Categories\{62C8FE65-4EBB-45E7-B440-6E39B2CDBF29}") -ne $true) {  New-Item "HKLM:\SOFTWARE\Classes\CLSID\{70A8A539-0204-4DB6-B52A-3B467A7F4134}\Implemented Categories\{62C8FE65-4EBB-45E7-B440-6E39B2CDBF29}" -force -ea SilentlyContinue };
        if((Test-Path -LiteralPath "HKLM:\SOFTWARE\Classes\SecureMFA_SupportTools.OTP") -ne $true) {  New-Item "HKLM:\SOFTWARE\Classes\SecureMFA_SupportTools.OTP" -force -ea SilentlyContinue };
        if((Test-Path -LiteralPath "HKLM:\SOFTWARE\Classes\SecureMFA_SupportTools.OTP\CLSID") -ne $true) {  New-Item "HKLM:\SOFTWARE\Classes\SecureMFA_SupportTools.OTP\CLSID" -force -ea SilentlyContinue };
        if((Test-Path -LiteralPath "HKLM:\SOFTWARE\Classes\CLSID\{DECA20B2-3F8B-376A-A9B0-DD56340ADB72}") -ne $true) {  New-Item "HKLM:\SOFTWARE\Classes\CLSID\{DECA20B2-3F8B-376A-A9B0-DD56340ADB72}" -force -ea SilentlyContinue };
        if((Test-Path -LiteralPath "HKLM:\SOFTWARE\Classes\CLSID\{DECA20B2-3F8B-376A-A9B0-DD56340ADB72}\InprocServer32") -ne $true) {  New-Item "HKLM:\SOFTWARE\Classes\CLSID\{DECA20B2-3F8B-376A-A9B0-DD56340ADB72}\InprocServer32" -force -ea SilentlyContinue };
        if((Test-Path -LiteralPath "HKLM:\SOFTWARE\Classes\CLSID\{DECA20B2-3F8B-376A-A9B0-DD56340ADB72}\InprocServer32\$dllversion") -ne $true) {  New-Item "HKLM:\SOFTWARE\Classes\CLSID\{DECA20B2-3F8B-376A-A9B0-DD56340ADB72}\InprocServer32\$dllversion" -force -ea SilentlyContinue };
        if((Test-Path -LiteralPath "HKLM:\SOFTWARE\Classes\CLSID\{DECA20B2-3F8B-376A-A9B0-DD56340ADB72}\ProgId") -ne $true) {  New-Item "HKLM:\SOFTWARE\Classes\CLSID\{DECA20B2-3F8B-376A-A9B0-DD56340ADB72}\ProgId" -force -ea SilentlyContinue };
        if((Test-Path -LiteralPath "HKLM:\SOFTWARE\Classes\CLSID\{DECA20B2-3F8B-376A-A9B0-DD56340ADB72}\Implemented Categories\{62C8FE65-4EBB-45E7-B440-6E39B2CDBF29}") -ne $true) {  New-Item "HKLM:\SOFTWARE\Classes\CLSID\{DECA20B2-3F8B-376A-A9B0-DD56340ADB72}\Implemented Categories\{62C8FE65-4EBB-45E7-B440-6E39B2CDBF29}" -force -ea SilentlyContinue };
        if((Test-Path -LiteralPath "HKLM:\SOFTWARE\Classes\SecureMFA_SupportTools.IDPAUTH") -ne $true) {  New-Item "HKLM:\SOFTWARE\Classes\SecureMFA_SupportTools.IDPAUTH" -force -ea SilentlyContinue };
        if((Test-Path -LiteralPath "HKLM:\SOFTWARE\Classes\SecureMFA_SupportTools.IDPAUTH\CLSID") -ne $true) {  New-Item "HKLM:\SOFTWARE\Classes\SecureMFA_SupportTools.IDPAUTH\CLSID" -force -ea SilentlyContinue };
        if((Test-Path -LiteralPath "HKLM:\SOFTWARE\Classes\CLSID\{FB55B541-0381-3ADC-8A5D-DB6169403C0A}") -ne $true) {  New-Item "HKLM:\SOFTWARE\Classes\CLSID\{FB55B541-0381-3ADC-8A5D-DB6169403C0A}" -force -ea SilentlyContinue };
        if((Test-Path -LiteralPath "HKLM:\SOFTWARE\Classes\CLSID\{FB55B541-0381-3ADC-8A5D-DB6169403C0A}\InprocServer32") -ne $true) {  New-Item "HKLM:\SOFTWARE\Classes\CLSID\{FB55B541-0381-3ADC-8A5D-DB6169403C0A}\InprocServer32" -force -ea SilentlyContinue };
        if((Test-Path -LiteralPath "HKLM:\SOFTWARE\Classes\CLSID\{FB55B541-0381-3ADC-8A5D-DB6169403C0A}\InprocServer32\$dllversion") -ne $true) {  New-Item "HKLM:\SOFTWARE\Classes\CLSID\{FB55B541-0381-3ADC-8A5D-DB6169403C0A}\InprocServer32\$dllversion" -force -ea SilentlyContinue };
        if((Test-Path -LiteralPath "HKLM:\SOFTWARE\Classes\CLSID\{FB55B541-0381-3ADC-8A5D-DB6169403C0A}\ProgId") -ne $true) {  New-Item "HKLM:\SOFTWARE\Classes\CLSID\{FB55B541-0381-3ADC-8A5D-DB6169403C0A}\ProgId" -force -ea SilentlyContinue };
        if((Test-Path -LiteralPath "HKLM:\SOFTWARE\Classes\CLSID\{FB55B541-0381-3ADC-8A5D-DB6169403C0A}\Implemented Categories\{62C8FE65-4EBB-45E7-B440-6E39B2CDBF29}") -ne $true) {  New-Item "HKLM:\SOFTWARE\Classes\CLSID\{FB55B541-0381-3ADC-8A5D-DB6169403C0A}\Implemented Categories\{62C8FE65-4EBB-45E7-B440-6E39B2CDBF29}" -force -ea SilentlyContinue };
        New-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\Classes\SecureMFA_SupportTools.SecureMFACOM_Class' -Name '(default)' -Value "SecureMFA_SupportTools.SecureMFACOM_Class" -PropertyType String -Force -ea SilentlyContinue;
        New-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\Classes\SecureMFA_SupportTools.SecureMFACOM_Class\CLSID' -Name '(default)' -Value "{70A8A539-0204-4DB6-B52A-3B467A7F4134}" -PropertyType String -Force -ea SilentlyContinue;
        New-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\Classes\CLSID\{70A8A539-0204-4DB6-B52A-3B467A7F4134}' -Name '(default)' -Value "SecureMFA_SupportTools.SecureMFACOM_Class" -PropertyType String -Force -ea SilentlyContinue;
        New-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\Classes\CLSID\{70A8A539-0204-4DB6-B52A-3B467A7F4134}\InprocServer32' -Name '(default)' -Value "mscoree.dll" -PropertyType String -Force -ea SilentlyContinue;
        New-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\Classes\CLSID\{70A8A539-0204-4DB6-B52A-3B467A7F4134}\InprocServer32' -Name 'ThreadingModel' -Value "Both" -PropertyType String -Force -ea SilentlyContinue;
        New-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\Classes\CLSID\{70A8A539-0204-4DB6-B52A-3B467A7F4134}\InprocServer32' -Name 'Class' -Value "SecureMFA_SupportTools.SecureMFACOM_Class" -PropertyType String -Force -ea SilentlyContinue;
        New-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\Classes\CLSID\{70A8A539-0204-4DB6-B52A-3B467A7F4134}\InprocServer32' -Name 'Assembly' -Value "SecureMFA_SupportTools, Version=$dllversion, Culture=neutral, PublicKeyToken=f1c44194ebb1b5d8" -PropertyType String -Force -ea SilentlyContinue;
        New-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\Classes\CLSID\{70A8A539-0204-4DB6-B52A-3B467A7F4134}\InprocServer32' -Name 'RuntimeVersion' -Value "v4.0.30319" -PropertyType String -Force -ea SilentlyContinue;
        New-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\Classes\CLSID\{70A8A539-0204-4DB6-B52A-3B467A7F4134}\InprocServer32\$dllversion' -Name 'Class' -Value "SecureMFA_SupportTools.SecureMFACOM_Class" -PropertyType String -Force -ea SilentlyContinue;
        New-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\Classes\CLSID\{70A8A539-0204-4DB6-B52A-3B467A7F4134}\InprocServer32\$dllversion' -Name 'Assembly' -Value "SecureMFA_SupportTools, Version=$dllversion, Culture=neutral, PublicKeyToken=f1c44194ebb1b5d8" -PropertyType String -Force -ea SilentlyContinue;
        New-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\Classes\CLSID\{70A8A539-0204-4DB6-B52A-3B467A7F4134}\InprocServer32\$dllversion' -Name 'RuntimeVersion' -Value "v4.0.30319" -PropertyType String -Force -ea SilentlyContinue;
        New-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\Classes\CLSID\{70A8A539-0204-4DB6-B52A-3B467A7F4134}\ProgId' -Name '(default)' -Value "SecureMFA_SupportTools.SecureMFACOM_Class" -PropertyType String -Force -ea SilentlyContinue;
        New-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\Classes\SecureMFA_SupportTools.OTP' -Name '(default)' -Value "SecureMFA_SupportTools.OTP" -PropertyType String -Force -ea SilentlyContinue;
        New-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\Classes\SecureMFA_SupportTools.OTP\CLSID' -Name '(default)' -Value "{DECA20B2-3F8B-376A-A9B0-DD56340ADB72}" -PropertyType String -Force -ea SilentlyContinue;
        New-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\Classes\CLSID\{DECA20B2-3F8B-376A-A9B0-DD56340ADB72}' -Name '(default)' -Value "SecureMFA_SupportTools.OTP" -PropertyType String -Force -ea SilentlyContinue;
        New-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\Classes\CLSID\{DECA20B2-3F8B-376A-A9B0-DD56340ADB72}\InprocServer32' -Name '(default)' -Value "mscoree.dll" -PropertyType String -Force -ea SilentlyContinue;
        New-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\Classes\CLSID\{DECA20B2-3F8B-376A-A9B0-DD56340ADB72}\InprocServer32' -Name 'ThreadingModel' -Value "Both" -PropertyType String -Force -ea SilentlyContinue;
        New-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\Classes\CLSID\{DECA20B2-3F8B-376A-A9B0-DD56340ADB72}\InprocServer32' -Name 'Class' -Value "SecureMFA_SupportTools.OTP" -PropertyType String -Force -ea SilentlyContinue;
        New-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\Classes\CLSID\{DECA20B2-3F8B-376A-A9B0-DD56340ADB72}\InprocServer32' -Name 'Assembly' -Value "SecureMFA_SupportTools, Version=$dllversion, Culture=neutral, PublicKeyToken=f1c44194ebb1b5d8" -PropertyType String -Force -ea SilentlyContinue;
        New-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\Classes\CLSID\{DECA20B2-3F8B-376A-A9B0-DD56340ADB72}\InprocServer32' -Name 'RuntimeVersion' -Value "v4.0.30319" -PropertyType String -Force -ea SilentlyContinue;
        New-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\Classes\CLSID\{DECA20B2-3F8B-376A-A9B0-DD56340ADB72}\InprocServer32\$dllversion' -Name 'Class' -Value "SecureMFA_SupportTools.OTP" -PropertyType String -Force -ea SilentlyContinue;
        New-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\Classes\CLSID\{DECA20B2-3F8B-376A-A9B0-DD56340ADB72}\InprocServer32\$dllversion' -Name 'Assembly' -Value "SecureMFA_SupportTools, Version=$dllversion, Culture=neutral, PublicKeyToken=f1c44194ebb1b5d8" -PropertyType String -Force -ea SilentlyContinue;
        New-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\Classes\CLSID\{DECA20B2-3F8B-376A-A9B0-DD56340ADB72}\InprocServer32\$dllversion' -Name 'RuntimeVersion' -Value "v4.0.30319" -PropertyType String -Force -ea SilentlyContinue;
        New-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\Classes\CLSID\{DECA20B2-3F8B-376A-A9B0-DD56340ADB72}\ProgId' -Name '(default)' -Value "SecureMFA_SupportTools.OTP" -PropertyType String -Force -ea SilentlyContinue;
        New-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\Classes\SecureMFA_SupportTools.IDPAUTH' -Name '(default)' -Value "SecureMFA_SupportTools.IDPAUTH" -PropertyType String -Force -ea SilentlyContinue;
        New-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\Classes\SecureMFA_SupportTools.IDPAUTH\CLSID' -Name '(default)' -Value "{FB55B541-0381-3ADC-8A5D-DB6169403C0A}" -PropertyType String -Force -ea SilentlyContinue;
        New-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\Classes\CLSID\{FB55B541-0381-3ADC-8A5D-DB6169403C0A}' -Name '(default)' -Value "SecureMFA_SupportTools.IDPAUTH" -PropertyType String -Force -ea SilentlyContinue;
        New-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\Classes\CLSID\{FB55B541-0381-3ADC-8A5D-DB6169403C0A}\InprocServer32' -Name '(default)' -Value "mscoree.dll" -PropertyType String -Force -ea SilentlyContinue;
        New-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\Classes\CLSID\{FB55B541-0381-3ADC-8A5D-DB6169403C0A}\InprocServer32' -Name 'ThreadingModel' -Value "Both" -PropertyType String -Force -ea SilentlyContinue;
        New-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\Classes\CLSID\{FB55B541-0381-3ADC-8A5D-DB6169403C0A}\InprocServer32' -Name 'Class' -Value "SecureMFA_SupportTools.IDPAUTH" -PropertyType String -Force -ea SilentlyContinue;
        New-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\Classes\CLSID\{FB55B541-0381-3ADC-8A5D-DB6169403C0A}\InprocServer32' -Name 'Assembly' -Value "SecureMFA_SupportTools, Version=$dllversion, Culture=neutral, PublicKeyToken=f1c44194ebb1b5d8" -PropertyType String -Force -ea SilentlyContinue;
        New-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\Classes\CLSID\{FB55B541-0381-3ADC-8A5D-DB6169403C0A}\InprocServer32' -Name 'RuntimeVersion' -Value "v4.0.30319" -PropertyType String -Force -ea SilentlyContinue;
        New-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\Classes\CLSID\{FB55B541-0381-3ADC-8A5D-DB6169403C0A}\InprocServer32\$dllversion' -Name 'Class' -Value "SecureMFA_SupportTools.IDPAUTH" -PropertyType String -Force -ea SilentlyContinue;
        New-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\Classes\CLSID\{FB55B541-0381-3ADC-8A5D-DB6169403C0A}\InprocServer32\$dllversion' -Name 'Assembly' -Value "SecureMFA_SupportTools, Version=$dllversion, Culture=neutral, PublicKeyToken=f1c44194ebb1b5d8" -PropertyType String -Force -ea SilentlyContinue;
        New-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\Classes\CLSID\{FB55B541-0381-3ADC-8A5D-DB6169403C0A}\InprocServer32\$dllversion' -Name 'RuntimeVersion' -Value "v4.0.30319" -PropertyType String -Force -ea SilentlyContinue;
        New-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\Classes\CLSID\{FB55B541-0381-3ADC-8A5D-DB6169403C0A}\ProgId' -Name '(default)' -Value "SecureMFA_SupportTools.IDPAUTH" -PropertyType String -Force -ea SilentlyContinue;


        #Restart RDS Gateway service
        if((Get-Service tsgateway -ErrorAction SilentlyContinue).Status -ne $null) {
        write-host "Restarting tsgateway service." -ForegroundColor Green
        Stop-Service tsgateway
        Start-Service tsgateway}

    }
    catch
    {
        Write-Host "$($MyInvocation.InvocationName): $_" -ForegroundColor red
    }    


}