Uninstall-SecureMFA_COM_Extensions.ps1

#Requires -RunAsAdministrator

<#
     .SYNOPSIS
        Uninstalls 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: 1.0.0.4
        Author: SecureMfa.com
        Creation Date: 03/12/2019
        Purpose/Change: Added COM extensions
   
    .EXAMPLE
        C:\PS> Uninstall-SecureMFA_COM_Extensions
 
        This command will uninstall SecureMFA_SupportTools.dll COM extensions from the server.
    
#>


$dllpath = (Join-Path -Path $PSScriptRoot -ChildPath SecureMFA_SupportTools.dll)

#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 Uninstall-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 "Uninstalling $dllpath from the server" -ForegroundColor Cyan

        #Remove SecureMfaOtpProvider DLL from GAC assembly
        $publish.GacRemove($dllpath)     
               
        #Remove SecureMFA COM Extension registry entries
        write-host "Removing SecureMFA COM Extension registry entries" -ForegroundColor Cyan
        if((Test-Path -LiteralPath "HKLM:\SOFTWARE\Classes\SecureMFA_SupportTools.SecureMFACOM_Class") -eq $true) {  Remove-Item -Path "HKLM:\SOFTWARE\Classes\SecureMFA_SupportTools.SecureMFACOM_Class" -Recurse -Force}

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


}