ps/Modules/Alkami.PowerShell.Database/Public/Split-ConnectionString.ps1

47 lines
1.9 KiB
PowerShell
Raw Normal View History

2023-05-30 22:51:22 -07:00
function Split-ConnectionString {
<#
.SYNOPSIS
Converts a connection string to a PowerShell object.
.DESCRIPTION
Splits the fields of a specified connection string into a PowerShell object using SqlConnectionStringBuilder, which can also return default values.
.PARAMETER ConnectionString
Required, the connection string to process.
.PARAMETER Full
Not required. Returns full Connection String object details, including default values for properties not specified in the provided connection string data.
.EXAMPLE
Split-ConnectionString -ConnectionString "data source=SL-Staging;Integrated Security=SSPI;Database=AlkamiMaster_Lane_PS1;MultiSubnetFailover=True"
DataSource IntegratedSecurity InitialCatalog MultiSubnetFailover
---------- ------------------ -------------- -------------------
SL-Staging True AlkamiMaster_Lane_PS1 True
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $true,
ValueFromPipeline = $true)]
[ValidateNotNullOrEmpty()]
[String]$ConnectionString,
[Parameter(Mandatory = $false)]
[Switch]$Full
)
$returnObject = @()
$returnObject = New-Object System.Data.SqlClient.SqlConnectionStringBuilder($ConnectionString)
# return $returnObject.psbase rather than the raw object due to how powershell handles overrides of the property names (translating "Data Source" to "DataSource", etc) so expected, simple property names are returned (DataSource, InitialCatalog, etc). See https://stackoverflow.com/a/61903683 for more info.
if ( $Full ) {
# if user requested all output, return it all
$returnObject.psbase
} else {
# otherwise just return the typical stuff
$returnObject.psbase | Select-Object DataSource,IntegratedSecurity,InitialCatalog,MultiSubnetFailover
}
}