ps/Modules/Alkami.PowerShell.Database/Public/Get-FormattedConnectionString.ps1
2023-05-30 22:51:22 -07:00

73 lines
2.1 KiB
PowerShell

function Get-FormattedConnectionString {
<#
.SYNOPSIS
Generate a formatted connection string for integrated security access
.DESCRIPTION
Generate a formatted connection string for integrated security access
.PARAMETER serverName
[string] Used to test connection to the server
.PARAMETER instanceName
[string] Instance name to use for testing
.PARAMETER databaseName
[string] Database name to check exists
.INPUTS
Server name, instance name, database name
.OUTPUTS
A connection string according to the expected inputs
.EXAMPLE
Get-FormattedConnectionString -ServerName . -DatabaseName DeveloperDynamic
Get-FormattedConnectionString -ServerName . -DatabaseName DeveloperDynamic
"Data Source=.\;Integrated Security=SSPI; Database=DeveloperDynamic;"
.EXAMPLE
Get-FormattedConnectionString . AlkamiMaster
Get-FormattedConnectionString . AlkamiMaster
"Data Source=.\;Integrated Security=SSPI; Database=AlkamiMaster;"
#>
[CmdletBinding()]
[OutputType([System.String])]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSPossibleIncorrectComparisonWithNull", "", Justification="Array Consolidation is Acceptable")]
param(
[Parameter(Mandatory=$true, Position=0)]
[string]$serverName,
[Parameter(Mandatory=$true, Position=1)]
[string]$databaseName,
[Parameter(Mandatory=$false, Position=2)]
[string]$instanceName
)
process {
if ([System.String]::IsNullOrEmpty($serverName) -and [System.String]::IsNullOrEmpty($instanceName)) {
Write-Verbose "No server or instance names passed in, returning default connection string for server"
return (Get-ConnectionString 'AlkamiMaster')
}
$serverName = ($serverName, "." -ne $null)[0]
$instanceName = ($instanceName, "" -ne $null)[0]
if ($serverName.EndsWith("\\")){
$serverName = $serverName.Substring(0, $serverName.Length - 1)
}
$connectionString = "Data Source=$serverName\$instanceName;Integrated Security=SSPI; Database=$databaseName;"
## TODO - bmorris - add multi cluster failover support
Write-Verbose "Found $connectionString"
return $connectionString
}
}