Link to home
Start Free TrialLog in
Avatar of Christian Palacios
Christian PalaciosFlag for Canada

asked on

Change a specific key value in app.config file with another value.

Hi there,

After I run a job, I need to replace this entire key in a app.config file, but I'm not sure exactly how to replace the entire key.  

<add key="ORMDBSessionFileOrConString" value="text to change" />

I need to be able to run a script (batch/VBscript/anything) that will look for this exact key entry and replace the value of it.  

Any ideas/suggestions?

Thanks in advance!
- Christian
Avatar of oBdA
oBdA

Use Powershell, and do it properly instead of a textual search and replace.
First, you'll need the "XPath"; you didn't post the complete XML, so you'll have to figure it out yourself.
It's basically  the node path down to the node you want to change.
Example:
<root>
	<config>
		<add key="ORMDBSessionFileOrConString" value="text to change" />
		<add key="WhateverElse" value="text to change" />
	</config>
</root>

Open in new window

To identify the node to change in the XML above, the XPath would be "root/config/add[@key='ORMDBSessionFileOrConString']".
The [@key=...] identifies the node to change by its attribute 'key', with the value 'ORMDBSessionFileOrConString'.
If the "add" node is the only one of that name in its parent node, you don't need the part in square brackets: "root/config/add" will do.
Note that node names, attributes names, and the attribute values in an XPath are CaSe SeNsItIvE.

Then it's easy:
$ConfigFile = 'C:\Whatever\app.config'
## XML Node names and Attributes are CaSe SeNsItIvE!
$XPath = "root/config/add[@key='ORMDBSessionFileOrConString']"
$Attribute = "value"
$NewValue = "The new text"

$xml = [xml](Get-Content -Path $ConfigFile)
$xml.SelectSingleNode($XPath).SetAttribute($Attribute, $NewValue)
$xml.Save($ConfigFile)

Open in new window

Avatar of Christian Palacios

ASKER

Thanks for your reply!  Here is the exact first lines in the .config file to the keys I need to modify:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="ORMDBSessionFileOrConString" value="data1" />
    <add key="TargetDBSessionFileOrConString" value="data2" />
..
..
  </appSettings>
</configuration>

So taking your example script, here's what I have:

##########################################################################
$ConfigFile = 'C:\Whatever\app.config'
## XML Node names and Attributes are CaSe SeNsItIvE!
$XPath = "configuration/appSettings/add[@key='ORMDBSessionFileOrConString']"
$Attribute = "value"
$NewValue = "The new text"

$xml = [xml](Get-Content -Path $ConfigFile)
$xml.SelectSingleNode($XPath).SetAttribute($Attribute, $NewValue)
$xml.Save($ConfigFile)
##########################################################################

As you can see above, I actually have two keys I need to update, one right after the other.  Do I need a separate PowerShell script for the second key or can I copy what's already in the script and run it twice, with different values for the $XPath, $NewValue?

Does the PowerShell script need anything else to run properly, or should this be it?

Thanks!
- Christian
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Awesome, thanks very much!  I'll give it a shot and test it out.  I'll report back.

- Christian
Works perfectly!  Thank you!
i have  a config file as below
<meeServices>
  <edmServers>
    <server name="EDMServer" description="EDMServer" username="" password="" integratedSecurity="true" />
  </edmServers>
  <rdmServers>
    <server name="EDMServer" description="EDMServer" username="" password="" integratedSecurity="true" />
  </rdmServers>
  <settings>
    <add name="ExposureSource" value="path" />
    <add name="DomainDatabase" value="Server=HeadNode;Initial Catalog=RMS_MEE_Data;Integrated Security=True;" />
    <add name="ResultsDatabase" value="Server=HeadNode;Initial Catalog=RMS_MEE_Results;Integrated Security=True;" />
    <add name="JobManagerService" value="url" />
    <add name="licensecode" value="code" />
    <add name="WhiteListedIPAddresses" value="*" />
  </settings>
</meeServices>

 in settings , i want to change the name which contains licensecode to newcode. how do i do this.

$ConfigFile = 'C:\Users\psman\Downloads\dev_qa.config'
## XML Node names and Attributes are CaSe SeNsItIvE!
$xml = [xml](Get-Content -Path $ConfigFile)

$xml.SelectSingleNode("meeservices/settings/add[@name='licensecode']").SetAttribute("name", "data1")

$xml.Save($ConfigFile)