Install-SecureMFAEmailOtpProvider.ps1

<#
     .SYNOPSIS
        Installs SecureMFA.com MFA Email OTP authentication provider for ADFS.
    .DESCRIPTION
        Deployment must be done from elevated PowerShell command window
        If you are using federation server farm that uses Windows Internal Database, you must start installation using the primary federation server of the farm as a MAIN node.
        Installation needs to be executed on ADFS farm server (not web application proxy servers).
 
        Dependencies:
            * System which executes a script must have Microsoft Framework 4.6.1 and above installed.
            * SecureMfaEmailOtpProvider.dll file must be present in script directory.
            * SecureMfaEmailOtpProvider.json configuration file must be present in script directory.
                                     
            Bellow is a sample of valid Json config file with minimal configuration required for installation:
                {
                  "company": "MyCompany",
                  "serialkey": "m00000000",
                  "smtp_server": "smtp.adatum.labnet",
                  "smtp_mailfrom": "mfa.no.reply@adatum.labnet",
                  "smtp_port": "25",
                  "smtp_enablessl": "false",
                  "smtp_username": "",
                  "smtp_password": "",
                  "smtp_remove_user_prefix": "false",
                  "auth_code_valid_inteval_seconds": "3600",
                  "auth_code_secret_unique": "false",
                  "auth_code_secret_AD_user_atribute": "primaryTelexNumber",
                  "ui_customization": "false",
                  "ui_login_text": "",
                  "ui_allowed_domains_for_otpcode": "",
                  "encryption_passphrase": "d9GhT=7=Ox8-+LaZ",
                  "verboselog": "false",
                }
 
    .PARAMETER NotMainNode
        NotMainNode parameter is required when you do installation on multiple ADFS nodes (not web application proxy servers).
        This needs to be executed on other adfs servers when installation of a provider is done on the MAIN(First) ADFS node.
 
    .NOTES
        Version: 1.0.1.1
        Author: SecureMfa.com
        Creation Date: 28/08/2019
        Purpose/Change: Incorporated into module
   
    .EXAMPLE
        C:\PS> Install-SecureMfaEmailOtpProvider
 
        This command will install OTP authentication provider on the MAIN ADFS node.
 
    .EXAMPLE
        C:\PS> Install-SecureMfaEmailOtpProvider -NotMainNode
          
        This command will install OTP authentication provider on OTHER ADFS node(s).
#>


$dllpath = (Join-Path -Path $PSScriptRoot -ChildPath SecureMfaEmailOtpProvider.dll)
#Spanish language
$dllpath_es = (Join-Path -Path $PSScriptRoot -ChildPath 'Languages\es\SecureMfaEmailOtpProvider.resources.dll')                  
#French language
$dllpath_fr = (Join-Path -Path $PSScriptRoot -ChildPath 'Languages\fr\SecureMfaEmailOtpProvider.resources.dll')  
#German language
$dllpath_de = (Join-Path -Path $PSScriptRoot -ChildPath 'Languages\de\SecureMfaEmailOtpProvider.resources.dll')
#Chinese language
$dllpath_zh = (Join-Path -Path $PSScriptRoot -ChildPath 'Languages\zh\SecureMfaEmailOtpProvider.resources.dll')  
#Portuguese language
$dllpath_pt = (Join-Path -Path $PSScriptRoot -ChildPath 'Languages\pt\SecureMfaEmailOtpProvider.resources.dll')
#Russian language
$dllpath_ru = (Join-Path -Path $PSScriptRoot -ChildPath 'Languages\ru\SecureMfaEmailOtpProvider.resources.dll') 
#Italian language
$dllpath_it = (Join-Path -Path $PSScriptRoot -ChildPath 'Languages\it\SecureMfaEmailOtpProvider.resources.dll') 
#Arabic language
$dllpath_ar = (Join-Path -Path $PSScriptRoot -ChildPath 'Languages\ar\SecureMfaEmailOtpProvider.resources.dll') 
#Turkish language
$dllpath_tr = (Join-Path -Path $PSScriptRoot -ChildPath 'Languages\tr\SecureMfaEmailOtpProvider.resources.dll')
#Dutch language
$dllpath_nl = (Join-Path -Path $PSScriptRoot -ChildPath 'Languages\nl\SecureMfaEmailOtpProvider.resources.dll')  
#Finnish language
$dllpath_fi = (Join-Path -Path $PSScriptRoot -ChildPath 'Languages\fi\SecureMfaEmailOtpProvider.resources.dll')
#Swedish language
$dllpath_sv = (Join-Path -Path $PSScriptRoot -ChildPath 'Languages\sv\SecureMfaEmailOtpProvider.resources.dll') 
#Norwegian language
$dllpath_no = (Join-Path -Path $PSScriptRoot -ChildPath 'Languages\no\SecureMfaEmailOtpProvider.resources.dll') 
#Polish language
$dllpath_pl = (Join-Path -Path $PSScriptRoot -ChildPath 'Languages\pl\SecureMfaEmailOtpProvider.resources.dll') 
#Danish language
$dllpath_da = (Join-Path -Path $PSScriptRoot -ChildPath 'Languages\da\SecureMfaEmailOtpProvider.resources.dll')
#Lithuanian language
$dllpath_lt = (Join-Path -Path $PSScriptRoot -ChildPath 'Languages\lt\SecureMfaEmailOtpProvider.resources.dll')

$configpath = (Join-Path -Path $PSScriptRoot -ChildPath SecureMfaEmailOtpProvider.json)

#Check if windows events source for application log exist, if not create one.
if ([System.Diagnostics.EventLog]::SourceExists("Secure MFA Email OTP") -eq $False) {New-EventLog -LogName "Application" -Source "Secure MFA Email OTP"}

#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-SecureMFAEmailOtpProvider {
Param
(
    [Parameter(Mandatory=$false, ParameterSetName="Default")]
    [Switch]$NotMainNode
)

    #OTHER nodes install
    if ($NotMainNode) {  

    write-host "Installing SecureMfaEmailOtpProvider Language Files on the OTHER node" -ForegroundColor Green

    #Remove SecureMfaEmailOtpProvider Languages DLL files from GAC assembly
    #Spanish language
    $publish.GacRemove($dllpath_es)               
    #French language
    $publish.GacRemove($dllpath_fr) 
    #German language
    $publish.GacRemove($dllpath_de)
    #Chinese language
    $publish.GacRemove($dllpath_zh)  
    #Portuguese language
    $publish.GacRemove($dllpath_pt)
    #Russian language
    $publish.GacRemove($dllpath_ru) 
    #Italian language
    $publish.GacRemove($dllpath_it)
    #Arabic language
    $publish.GacRemove($dllpath_ar) 
    #Turkish language
    $publish.GacRemove($dllpath_tr)
    #Dutch language
    $publish.GacRemove($dllpath_nl)
    #Finnish language
    $publish.GacRemove($dllpath_fi)
    #Swedish language
    $publish.GacRemove($dllpath_sv)
    #Norwegian language
    $publish.GacRemove($dllpath_no)
    #Polish language
    $publish.GacRemove($dllpath_pl) 
    #Danish language
    $publish.GacRemove($dllpath_da)
    #Lithuanian language
    $publish.GacRemove($dllpath_lt)
    
    #Add SecureMfaEmailOtpProvider Languages DLL files to GAC assembly
    #Spanish language
    $publish.GacInstall($dllpath_es)               
    #French language
    $publish.GacInstall($dllpath_fr) 
    #German language
    $publish.GacInstall($dllpath_de)
    #Chinese language
    $publish.GacInstall($dllpath_zh)  
    #Portuguese language
    $publish.GacInstall($dllpath_pt)
    #Russian language
    $publish.GacInstall($dllpath_ru) 
    #Italian language
    $publish.GacInstall($dllpath_it)
    #Arabic language
    $publish.GacInstall($dllpath_ar) 
    #Turkish language
    $publish.GacInstall($dllpath_tr)
    #Dutch language
    $publish.GacInstall($dllpath_nl)
    #Finnish language
    $publish.GacInstall($dllpath_fi)
    #Swedish language
    $publish.GacInstall($dllpath_sv)
    #Norwegian language
    $publish.GacInstall($dllpath_no)
    #Polish language
    $publish.GacInstall($dllpath_pl) 
    #Danish language
    $publish.GacInstall($dllpath_da)
    #Lithuanian language
    $publish.GacInstall($dllpath_lt)

    write-host "Installing SecureMfaEmailOtpProvider on the OTHER node" -ForegroundColor Cyan

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

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

    #Restart ADFS service
    write-host "Restarting adfssrv service." -ForegroundColor Green
    Stop-Service adfssrv
    Start-Service adfssrv

    }

    #MAIN nodes install
    else {

    write-host "Installing SecureMfaEmailOtpProvider on the MAIN node using configuration $configpath" -ForegroundColor Cyan
    #Remove additional authentication providers from ADFS global policy and unregister SecureMfaEmailOtpProvider
    write-host "Removing additional authentication providers from ADFS global policy and unregistering SecureMfaEmailOtpProvider." -ForegroundColor Green
    Set-AdfsGlobalAuthenticationPolicy -AdditionalAuthenticationProvider ""
    unregister-AdfsAuthenticationProvider -Name �SecureMfaEmailOtpProvider� -Confirm:$false

    #Restart ADFS service
    write-host "Restarting adfssrv service." -ForegroundColor Green
    Stop-Service adfssrv
    Start-Service adfssrv
    
    write-host "Installing SecureMfaEmailOtpProvider Language Files..." -ForegroundColor Green

   #Remove SecureMfaEmailOtpProvider Languages DLL files from GAC assembly
    #Spanish language
    $publish.GacRemove($dllpath_es)
    write-host "Removing: $dllpath_es"               
    #French language
    $publish.GacRemove($dllpath_fr) 
    #German language
    $publish.GacRemove($dllpath_de)
    #Chinese language
    $publish.GacRemove($dllpath_zh)  
    #Portuguese language
    $publish.GacRemove($dllpath_pt)
    #Russian language
    $publish.GacRemove($dllpath_ru) 
    #Italian language
    $publish.GacRemove($dllpath_it)
    #Arabic language
    $publish.GacRemove($dllpath_ar) 
    #Turkish language
    $publish.GacRemove($dllpath_tr)
    #Dutch language
    $publish.GacRemove($dllpath_nl)
    #Finnish language
    $publish.GacRemove($dllpath_fi)
    #Swedish language
    $publish.GacRemove($dllpath_sv)
    #Norwegian language
    $publish.GacRemove($dllpath_no)
    #Polish language
    $publish.GacRemove($dllpath_pl) 
    #Danish language
    $publish.GacRemove($dllpath_da)
    #Lithuanian language
    $publish.GacRemove($dllpath_lt)
    
    #Add SecureMfaEmailOtpProvider Languages DLL files to GAC assembly
    #Spanish language
    $publish.GacInstall($dllpath_es)
    write-host "Adding: $dllpath_es"                
    #French language
    $publish.GacInstall($dllpath_fr) 
    #German language
    $publish.GacInstall($dllpath_de)
    #Chinese language
    $publish.GacInstall($dllpath_zh)  
    #Portuguese language
    $publish.GacInstall($dllpath_pt)
    #Russian language
    $publish.GacInstall($dllpath_ru) 
    #Italian language
    $publish.GacInstall($dllpath_it)
    #Arabic language
    $publish.GacInstall($dllpath_ar) 
    #Turkish language
    $publish.GacInstall($dllpath_tr)
    #Dutch language
    $publish.GacInstall($dllpath_nl)
    #Finnish language
    $publish.GacInstall($dllpath_fi)
    #Swedish language
    $publish.GacInstall($dllpath_sv)
    #Norwegian language
    $publish.GacInstall($dllpath_no)
    #Polish language
    $publish.GacInstall($dllpath_pl) 
    #Danish language
    $publish.GacInstall($dllpath_da)
    #Lithuanian language
    $publish.GacInstall($dllpath_lt)

    write-host "Installing SecureMfaEmailOtpProvider..." -ForegroundColor Green

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

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

    #Register SecureMfaEmailOtpProvider addapter
    $typeName = �SecureMfaEmailOtpProvider.AuthenticationAdapter, SecureMfaEmailOtpProvider, Version=1.0.1.1, Culture=neutral, PublicKeyToken=1ecd877c866018d2, processorArchitecture=MSIL�
    Register-AdfsAuthenticationProvider -TypeName $typeName -Name �SecureMfaEmailOtpProvider� -ConfigurationFilePath $configpath

    #Restart ADFS service
    write-host "Restarting adfssrv service." -ForegroundColor Green
    Stop-Service adfssrv
    Start-Service adfssrv

    #Add SecureMfaEmailOtpProvider as additional authentication provider in ADFS
    write-host "Setting SecureMfaEmailOtpProvider as additional authentication provider in global authentication policy." -ForegroundColor Green
    Set-AdfsGlobalAuthenticationPolicy -AdditionalAuthenticationProvider "SecureMfaEmailOtpProvider"   
    
    }

    #List all authentication providers
    Get-AdfsAuthenticationProvider | select name

}