Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
51 lines (37 sloc) 1.36 KB
author comments layout slug title desc tags
Joe Kampschmidt
true
post
powershell-script-update-webconfig
Simple Powershell script to replace values in a .net configuration file
A simple powershell script to update a .net based xml web.config or app.config with environment specific values.
devops
powershell
xml

The following powershell script shows how to perform quick and dirty .net XML config file swapping. I use these quick swaps for our build server and continous deployments in Bamboo. This should work on Powershell version 2+.

There are two functions below:

  1. One for finding and replacing the key value pairs for AppSettings.
  2. Replacing the log4net RollingFileAppender filename.
# swap a key/value in AppSettings
Function swapAppSetting {
  param([string]$key,[string]$value )
  $obj = $doc.configuration.appSettings.add | where {$_.Key -eq $key }
  $obj.value = $value
}

# swap the log4net filename
Function swaplog4Net {
  param([string]$value )
  $fileAppender = $doc.configuration.log4net.appender | where {$_.Type -eq 'log4net.Appender.RollingFileAppender' }
  $log = $fileAppender.file
  $log.value = $value
}


$webConfig = "\\server\path\app.config"
$doc = [Xml](Get-Content $webConfig)

swaplog4Net 'server-custom-name.log'
swapAppSetting 'env' 'test'

$doc.Save($webConfig)

Hopefully this saves you a few minutes.