Skip to content

Instantly share code, notes, and snippets.

@gavi
Created January 17, 2019 16:40
Show Gist options
  • Save gavi/dcc14ffc60296bf61299fe42a65840d3 to your computer and use it in GitHub Desktop.
Save gavi/dcc14ffc60296bf61299fe42a65840d3 to your computer and use it in GitHub Desktop.
Add a connectionString to Web.config
function AddAttribute([System.Xml.XmlNode] $node, $name , $value){
$attrib = $node.OwnerDocument.CreateAttribute($name)
$attrib.Value = $value
$node.Attributes.Append($attrib)
}
function AddConnectionString($webconfigFile, $name, $connectionString){
[xml]$document = Get-Content $webconfigFile
$configElement = $document.SelectSingleNode("configuration")
$connectionStringsElement = $configElement.SelectSingleNode("connectionStrings")
[System.Xml.XmlNode]$add = $document.CreateElement("add")
AddAttribute $add "name" $name
AddAttribute $add "connectionString" $connectionString
$connectionStringsElement.AppendChild($add)
$document.Save($webconfigFile)
}
AddConnectionString C:\temp\2.3.48.1547065790761\api\Web.config "test" "testConnectionString"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment