Update-SecureMFA_WEPAPI_Portal.ps1
#Requires -RunAsAdministrator #Requires -Version 5.0 <# .SYNOPSIS Updates SecureMFA WEB API Portal configuration. .DESCRIPTION Updates SecureMFA WEB API Portal configuration in web.config file. Dependencies: * Only licensed Providers API requests will allow to use customised data_encryption and ui_customization settings; unlicensed providers API requests will be subject to default application values when encrypting response data even passphrase value will be updated in web.config file. Any other application settings can be changed as required. * Default values are: api_headers_value = "P4WK6mUMgL6ztXtiJUurA3Fhn5Xjbejy1ZAhwokT", data_encryption = $false, data_encryption_passphrase = "d9GhT=7=Ox8-+LaZ", ui_customization = $false, ui_login_failures = 0, ui_lockout_minutes = 5, smtp_server = "smtp.adatum.labnet", smtp_mailfrom" = "mfa.no.reply@adatum.labnet", smtp_port = 25, smtp_enablessl = $false, smtp_username = "", smtp_password = "", verboselog = $false .NOTES Version: 2.0.0.1 Author: SecureMfa.com Creation Date: 28/08/2020 Purpose/Change: Release .EXAMPLE C:\PS> Update-SecureMFA_WEPAPI_Portal -api_auth_endpoint_enabled $true -api_headers_value "P4WK6mUMgL6ztXtiJUurA3Fhn5Xjbejy1ZAhwokT" This command will update <appSettings> section with values from parameters in web.config file for SecureMFA WEB API Portal on a server. Any undefined parameters will be set to default values. #> #Check if windows events source for application log exist, if not create one. if ([System.Diagnostics.EventLog]::SourceExists("Secure MFA WEBAPI") -eq $False) {New-EventLog -LogName "Application" -Source "Secure MFA WEBAPI" ; Write-Host "Secure MFA WEBAPI Log Source Created."} Function Update-SecureMFA_WEPAPI_Portal { Param ( [Parameter(Mandatory=$false)][string]$api_headers_value = "P4WK6mUMgL6ztXtiJUurA3Fhn5Xjbejy1ZAhwokT", [Parameter(Mandatory=$false)][bool]$data_encryption = $false, [Parameter(Mandatory=$false)][string]$data_encryption_passphrase = "d9GhT=7=Ox8-+LaZ", [Parameter(Mandatory=$false)][bool]$ui_customization = $false, [Parameter(Mandatory=$false)][int]$ui_login_failures = 0, [Parameter(Mandatory=$false)][int]$ui_lockout_minutes = 5, [Parameter(Mandatory=$false)][string]$smtp_server = "smtp.adatum.labnet", [Parameter(Mandatory=$false)][string]$smtp_mailfrom = "mfa.no.reply@adatum.labnet", [Parameter(Mandatory=$false)][int]$smtp_port = 25, [Parameter(Mandatory=$false)][bool]$smtp_enablessl = $false, [Parameter(Mandatory=$false)][string]$smtp_username, [Parameter(Mandatory=$false)][string]$smtp_password, [Parameter(Mandatory=$false)][bool]$verboselog = $false, [Parameter(Mandatory=$false)][string]$siteName = "SecureMFAWebAPI", [Parameter(Mandatory=$false)][string]$WebAPIPortalPath = "C:\inetpub\SecureMFAWebAPI\", [Parameter(Mandatory=$false)][string]$IISAppPoolName = "SecureMFAWebAPI", [Parameter(Mandatory=$false)][Switch]$Force ) if (!$Force) { $message = "Do you want update <appSettings> values for " + $WebAPIPortalPath + "Web.Config ?"; $question = 'Please confirm?' $choices = New-Object Collections.ObjectModel.Collection[Management.Automation.Host.ChoiceDescription] $choices.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList '&Yes')) $choices.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList '&No')) $decision_Validation = $Host.UI.PromptForChoice($message, $question, $choices, 0) if ($decision_Validation -eq 1 ) {Write-Host "Web.config update has been cancelled, exiting!" -ForegroundColor Yellow ; break} } try { $Error.Clear() if (!(Test-Path $WebAPIPortalSource -Type Leaf) ) { throw "$WebAPIPortalSource does not exist" ; break} #Start update #Apply connection string into web.config #Connection string update and replace $test = (Get-Content -path ($WebAPIPortalPath + "Web.Config") -Raw) $newtest = "<appSettings>APPLICATIONSSETTINGSPLACEHOLDER</appSettings>" $pattern = "(?s)<appSettings>(.*?)</appSettings>" $result0 = [regex]::match($test, $pattern).Groups[1].Value $result1 = [regex]::match($newtest, $pattern).Groups[1].Value $test -replace [regex]::Escape($result0), $result1.Replace('$', '$$') | Set-Content -Path ($WebAPIPortalPath + "Web.Config") $test.Replace($result0, $result1) | Set-Content -Path ($WebAPIPortalPath + "Web.Config") $appsettingsvalues ="<add key=`"api_headers_value`" value=`"" + $api_headers_value + "`" /><add key=`"data_encryption`" value=`"" + $data_encryption + "`" /><add key=`"data_encryption_passphrase`" value=`"" + $data_encryption_passphrase + "`" /><add key=`"ui_customization`" value=`"" + $ui_customization + "`" /><add key=`"ui_login_failures`" value=`"" + $ui_login_failures + "`" /><add key=`"ui_lockout_minutes`" value=`"" + $ui_lockout_minutes + "`" /><add key=`"smtp_server`" value=`"" + $smtp_server +"`" /><add key=`"smtp_mailfrom`" value=`"" + $smtp_mailfrom +"`" /><add key=`"smtp_port`" value=`"" + $smtp_port +"`" /><add key=`"smtp_enablessl`" value=`"" + $smtp_enablessl +"`" /><add key=`"smtp_username`" value=`"" + $smtp_username +"`" /><add key=`"smtp_password`" value=`"" + $smtp_password +"`" /><add key=`"verboselog`" value=`"" + $verboselog + "`" />" (Get-Content -path ($WebAPIPortalPath + "Web.Config") -Raw) -replace 'APPLICATIONSSETTINGSPLACEHOLDER',$appsettingsvalues| Set-Content -Path ($WebAPIPortalPath + "Web.Config") # Complete write-host "Update of $WebAPIPortalPath Web.Config has been complete." -ForegroundColor Cyan #Get <appSettings> values Get-WebConfigurationProperty -pspath "iis:\Sites\$siteName" -filter "/appSettings/add" -name * | select key,value } catch { Write-Host "$($MyInvocation.InvocationName): $_" -ForegroundColor red } } |