Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cchitsiang/9201105 to your computer and use it in GitHub Desktop.
Save cchitsiang/9201105 to your computer and use it in GitHub Desktop.
Set Connection String Powershell Function Usage: Set-ConnectionString "MyWebApp\app.config" MyConnectionString "Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;" Other Reference for more scenario : http://www.protosystem.net/post/2009/06/01/Using-Powershell-to-manage-application-configuration.aspx
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