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
Christian PalaciosSenior IT Systems AdministratorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
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

0
Christian PalaciosSenior IT Systems AdministratorAuthor Commented:
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
0
oBdACommented:
No need for two scripts; you can just copy the relevant parts; saving the xml would obviously be only required once.
Or you can hard-code the paths; the variables were there mainly to separate the settings from the code, but it's totally up to you.
##########################################################################
$ConfigFile = 'C:\Whatever\app.config'
## XML Node names and Attributes are CaSe SeNsItIvE!
$xml = [xml](Get-Content -Path $ConfigFile)

$xml.SelectSingleNode("configuration/appSettings/add[@key='ORMDBSessionFileOrConString']").SetAttribute("value", "data1")
$xml.SelectSingleNode("configuration/appSettings/add[@key='TargetDBSessionFileOrConString']").SetAttribute("value", "data2")

$xml.Save($ConfigFile)
##########################################################################

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Christian PalaciosSenior IT Systems AdministratorAuthor Commented:
Awesome, thanks very much!  I'll give it a shot and test it out.  I'll report back.

- Christian
0
Christian PalaciosSenior IT Systems AdministratorAuthor Commented:
Works perfectly!  Thank you!
0
Manikanta SrinivasCommented:
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)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.