Update configSource of XML element in web.config using Powershell by passing in Parameters

Posted by Howard on Stack Overflow See other posts from Stack Overflow or by Howard
Published on 2009-08-25T14:05:16Z Indexed on 2010/04/20 21:33 UTC
Read the original article Hit count: 374

Filed under:
|
|

I am trying to figure out a way to update my web.config for different environments by updating the configSource for the appSettings element in the web.config.

Here are the way I know how to do it.

$xml.get_DocumentElement().appSettings.configSource = $replaced_test

The problem is that I want one base script where I can pass in different nodes to the script that I want to change and update but I am not sure how to do it.

For example, I want to be able to call a powershell script like this

changeWebConfig.ps1 nodeToChange newValueofNode

I hope this was clear enough.

This is the code I have now.

$webConfigPath = "C:\web.config"   

# Get the content of the config file and cast it to XML 
$xml = [xml](get-content $webConfigPath) 

#this was the trick I had been looking for  
$root = $xml.get_DocumentElement()."system.serviceModel".client.configSource  = $replace

# Save it  
$xml.Save($webConfigPath)

© Stack Overflow or respective owner

Related posts about powershell

Related posts about web-config