Skip to content

Instantly share code, notes, and snippets.

@mrichman
Forked from jwcarroll/modifyConnectionStrings.ps1
Created February 27, 2017 14:53
Show Gist options
  • Save mrichman/e74381ceb87ee0595be8f264126632be to your computer and use it in GitHub Desktop.
Save mrichman/e74381ceb87ee0595be8f264126632be to your computer and use it in GitHub Desktop.
Set Connection String Powershell Function
Function Set-ConnectionString{
[CmdletBinding(SupportsShouldProcess=$True)]
Param(
[string]$fileName="app.config",
[string]$connectionStringName,
[string]$connectionString
)
$config = [xml](Get-Content -LiteralPath $fileName)
$config.Configuration.connectionStrings
$connStringElement = $config.SelectSingleNode("configuration/connectionStrings/add[@name='$connectionStringName']")
if($connStringElement) {
$connStringElement.connectionString = $connectionString
if($pscmdlet.ShouldProcess("$fileName","Modify app.config connection string")){
Write-Host ("Updating app.config connection string {0} to be {1}" -f $connectionStringName, $connectionString)
$config.Save($fileName)
}
}
else{
Write-Error "Unable to locate connection string named: $connectionStringName"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment