Uninstall-SecureMFA_RD_Gateway_Authentication_Provider.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#Requires -RunAsAdministrator

<#
     .SYNOPSIS
        Uninstalls SecureMFA RD Gateway OTP Authentication Provider.
    .DESCRIPTION
        SecureMFA RD Gateway OTP Authentication Provider plugin allows Microsoft RDS Gateway server to work with One-Time Pass codes which are elivered by RDP client as authentication cookie during user�s logon.
 
        Dependencies:
            * SecureMFA Tools COM extensions are required to be present on RDS gateway server to enable extraction of OTP codes for this service from SecureMFA database.
            * 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.3
        Author: SecureMfa.com
        Creation Date: 17/12/2019
        Purpose/Change: Minor changes
   
    .EXAMPLE
        C:\PS> Uninstall-SecureMFA_RDG_OTP_AuthenticationProvider
 
        This command will uninstall SecureMFA RD Gateway OTP Authentication Provider plugin from the server.
    
#>


$dllpath = (Join-Path -Path $PSScriptRoot -ChildPath sMFARDGAuthenticationProvider.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."}


Function Uninstall-SecureMFA_RDG_OTP_AuthenticationProvider {
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 "RD Gateway Authentication Provider requires RDS Gateway service to be installed. TS Gateway services doesn't exist on $env:COMPUTERNAME" -ForegroundColor Yellow; break}
    
    try
    {
        $Error.Clear()
        if (!(Test-Path $dllpath -Type Leaf) ) { throw "$dllpath does not exist" }

        write-host "Registering Authentication provider: $dllpath" -ForegroundColor Cyan

        #Change TS Gataway Authentication Plugin back to native

        (get-WMIObject "Win32_TSGatewayServerSettings" -computer "." -Namespace "ROOT\CIMV2\TerminalServices").SetAuthenticationPluginAndRecycleRpcApplicationPools("native")

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

        #Register Authentication providers DLL

        Invoke-Command -ScriptBlock { regsvr32 /u /s $args[0] } -ArgumentList $dllpath

        # Remove SecureMFA RD Gateway OTP Authentication Provider registry
        write-host "Removing SecureMFA RD Gateway OTP Authentication Provider registry entries" -ForegroundColor Cyan
        if((Test-Path -LiteralPath "HKLM:\SOFTWARE\SecureMFA") -eq $true) { $keyPath = 'HKLM:\SOFTWARE\SecureMFA'; Remove-ItemProperty -Path $keyPath -Name rds* -Force; };

        Write-host "RD Gateway Authentication Plugig has been configured: "(Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Terminal Server Gateway\Authentication plug-ins').'(default)' -ForegroundColor Cyan
        
    }
    catch
    {
        Write-Host "$($MyInvocation.InvocationName): $_" -ForegroundColor red
    }    


}