ps/Modules/Alkami.PowerShell.Configuration/Public/Set-RedisConnectionString.ps1

103 lines
4.0 KiB
PowerShell
Raw Permalink Normal View History

2023-05-30 22:51:22 -07:00
function Set-RedisConnectionString{
<#
.SYNOPSIS
Sets the redis connection string in the machine config, and removes
the legacy app setting redis connection.
.DESCRIPTION
Overwrites any redis connection strings in configuration.connectionstrings
Removes legacy redis connections located in configuration.appsettings
.PARAMETER RedisFQDN
String, the full qualified domain name of the redis server.
.PARAMETER Port
The port for the redis server.
.PARAMETER Password
String, the password for the redis server.
.PARAMETER use64bit
Boolean, passed to Get-DotNetConfigPath - if set to true, will return the filepath
of the 64 bit machine config file.
.EXAMPLE
Set-RedisConnectionString -RedisFQDN "redis-19226.redisstg-gen4.fh.local" -Password "P@ssw0rd" -Port 19226
Base usage, will set the redis connection string for this machine to
<add name="RedisSetting" connectionString="redis-19226.redisstg-gen4.fh.local:19226,keepAlive=60,password=P@ssw0rd" />
and remove the legacy connection located in app settings.
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$True)]
[Alias("ComputerName","Server")]
[string]$RedisFQDN,
[Parameter(Mandatory=$True)]
$Port,
[Parameter(Mandatory=$False)]
[string]$Password,
[Parameter(Mandatory=$False)]
[bool]$use64Bit = $True,
[Parameter(Mandatory=$False)]
[bool]$useSSL = $False
)
begin{
$logLead = (Get-LogLeadName);
Write-Verbose "$logLead Getting Maching.Config path, use64Bit set to $use64Bit"
$machineConfigPath = Get-DotNetConfigPath -use64Bit $use64Bit
if(!$machineConfigPath){throw "Machine config path could not be found"}
Write-Verbose "$logLead Reading machine config file located at $machineConfigPath"
$machineConfig = Read-XMLFile $machineConfigPath
if(!$machineConfig){throw "Machine config at $machineConfigPath could not be converted to xml"}
Write-Verbose "$logLead Constructing redis xml string"
if($Password){$PasswordString = ",password=$Password"}
$sslString = "";
if($useSSL)
{
$sslString = ",ssl=true"
}
$RedisXmlString = '<add name="RedisSetting" connectionString="{0}:{1},keepAlive=60{2}{3}" />'
$RedisXmlString = $RedisXmlString -f $RedisFQDN,$Port,$PasswordString,$sslString
}
process{
Write-Verbose "$logLead Ensuring configuration and connection string nodes exist"
if(!$machineConfig.configuration){
[void]$machineConfig.AppendChild($machineConfig.CreateNode("element","configuration", $null))
}
if(!$machineConfig.configuration.connectionStrings){
[void]$machineConfig.SelectSingleNode("configuration").AppendChild($machineConfig.CreateElement("connectionStrings"))
}
Write-Verbose "$logLead Removing current redis string"
$connectionStrings = $machineConfig.configuration.SelectSingleNode("connectionStrings")
if($connectionStrings.SelectNodes("add").count -gt 0){
$CurrentRedisSettings = $connectionStrings.add | Where-Object {$_.Name -eq "RedisSetting"}
$CurrentRedisSettings | ForEach-Object {[void]$connectionStrings.RemoveChild($_)}
}
Write-Verbose "$logLead Setting new redis string"
$RedisXmlDoc = [xml]($RedisXmlString)
$RedisNode = $machineConfig.ImportNode($RedisXmlDoc.FirstChild, $true)
[void]$connectionStrings.AppendChild($RedisNode)
Write-Verbose "$logLead Removing legacy redis string, if exists"
$appSettings = $machineConfig.configuration.appSettings
if($appSettings){
if($appSettings.SelectNodes("add").count -gt 0){
$LegacyRedisSettings = $appSettings.add | Where-Object {$_.key -eq "RedisHostCommaSeperatedEndpointsWithPorts"}
$LegacyRedisSettings | ForEach-Object { [void]$appSettings.RemoveChild($_)}
}
}
Write-Verbose "$logLead Saving machine config to path $machineConfigPath"
$machineConfig.Save($machineConfigPath)
}
}