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?
 
oBdAConnect With a Mentor Commented:
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
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.