Skip to content

Instantly share code, notes, and snippets.

@simplyadrian
Created May 14, 2015 14:30
Show Gist options
  • Save simplyadrian/d52e877141dcc09630d6 to your computer and use it in GitHub Desktop.
Save simplyadrian/d52e877141dcc09630d6 to your computer and use it in GitHub Desktop.
Add a connection string to a IIS web.config
# Powershell 2.0
# Stop and fail script when a command fails.
$errorActionPreference = "Stop"
# load library functions
$rsLibDstDirPath = "$env:rs_sandbox_home\RightScript\lib"
. "$rsLibDstDirPath\tools\PsOutput.ps1"
. "$rsLibDstDirPath\tools\ResolveError.ps1"
try
{
$appName = $env:OPT_CONNECTION_STRING_PROV_NAME
$connStringName = $env:OPT_CONNECTION_STRING_NAME
$dbServerName = $env:OPT_CONNECTION_STRING_DB_SERVER_NAME
$dbName = $env:OPT_CONNECTION_STRING_DB_NAME
$userPassword = $env:OPT_CONNECTION_STRING_DB_USER_PASSWORD
$userID = $env:OPT_CONNECTION_STRING_DB_USER_ID
$failoverPatner = $env:OPT_CONNECTION_STRING_FAILOVER_PARTNER_NAME
# Check inputs
if (!$env:RS_IIS_LAST_RELEASE -or !(Test-Path $env:RS_IIS_LAST_RELEASE))
{
throw "Unable to locate last release dir '$env:RS_IIS_LAST_RELEASE' - exiting..."
}
$webConfigPath = Join-Path $env:RS_IIS_LAST_RELEASE "web.config"
if (!(Test-Path $webConfigPath))
{
throw "Failed - file $webConfigPath not found."
}
### Add connection string to web.config
$xml = [xml](get-content $webConfigPath)
if ($connStringName)
{
Write-Host "Search for $connString connection string in web.config..."
foreach($connString in $xml.configuration.connectionStrings.add)
{
if ($connString.name -eq $connStringName)
{
Write-Host "Deleting connection string $($connString.name): $($connString.connectionString)"
$xml.configuration.connectionStrings.RemoveChild($connString) | Out-Null
}
}
}
Write-Host "Adding new connection string to web.config..."
# Create element for new connection string
$elem = $xml.CreateElement("add", $xml.DocumentElement.NamespaceURI)
if ($failoverPatner)
{
$newConnStr = "Server=$dbServerName;Failover Partner=$failoverPatner;Connection Timeout=60;Database=$dbName;User ID=$UserID;Password=$userPassword;"
}
else
{
$newConnStr = "Server=$dbServerName;Database=$dbName;User ID=$UserID;Password=$userPassword;"
}
Write-Host "New connection string: $newConnStr"
# Add name attribute
if ($connStringName)
{
$elem.SetAttribute('name', $connStringName)
}
# Add connection string itself
$elem.SetAttribute('connectionString', $newConnStr)
# Add provider name
if ($env:OPT_CONNECTION_STRING_PROV_NAME)
{
$elem.SetAttribute('providerName', $env:OPT_CONNECTION_STRING_PROV_NAME)
}
$elem.RemoveAttribute('xmlns')
# Append new element and save the file
# Important:
# - SelectSingleNode is needed because in some cases default navigation returns String
# instead of XMlElement
# - using namespace-independent XPath expression to select connectionStrings
# node by matching tag name
$xml.configuration.SelectSingleNode("//*[local-name()='connectionStrings']").AppendChild($elem) | Out-Null
$xml.Save($webConfigPath)
Write-Host "Connection string is added successfully."
}
catch
{
ResolveError
exit 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment