Skip to content

Instantly share code, notes, and snippets.

@rdbartram
Last active November 22, 2017 09:13
Show Gist options
  • Save rdbartram/9478ed7bfe7436f3c0e03d5403da4ecc to your computer and use it in GitHub Desktop.
Save rdbartram/9478ed7bfe7436f3c0e03d5403da4ecc to your computer and use it in GitHub Desktop.
Accepts SQL Connection strings and converts them to and from powershell objects for easy manipulation and use
function ConvertFrom-SQLDatabaseConnectionString {
[OutputType([PSCustomObject])]
[cmdletbinding()]
param(
[Parameter(mandatory, Valuefrompipeline)]
[string]
$ConnectionString
)
begin {
$hash = @{}
}
process {
$ConnectionString.Split(";") | % {
$d = $_.Split("=").Trim()
$hash.Add($d[0], $d[1])
}
}
end {
return [PSCustomObject]$hash
}
}
function ConvertTo-SQLDatabaseConnectionString {
[OutputType([string])]
[cmdletbinding()]
param(
[Parameter(mandatory, Valuefrompipeline)]
[PSCustomObject]
$ConnectionStringObject
)
begin {
$Connstring = ""
}
process {
$ConnectionStringObject | get-member | ? MemberType -ne "method" | % {
$Connstring += [string]::Format("{0}={1};", $_.name, $ConnectionStringObject."$($_.Name)")
}
}
end {
return $Connstring
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment