0

I have an XML config loaded into my script as an xml object called $appConfig. I am trying to replace the connection string value with a string of my own. This is what I have so far which finds the target string:

$appConfig.configuration.connectionStrings.add |
    ? {$_.name -eq $dbName} |
    select connectionString

I essentially want something akin to this:

$appConfig.configuration.connectionStrings.add |
    ? {$_.name -eq $dbName} |
    select connectionString = $updatedConnectionString

I'm pretty sure I need to make a variable and populate it with the $appConfig but everytime I try I just end up with another XML object instead of editing the target $appConfig object as intended. I write the $appConfig back to its original file location and complete the edit. I just can't seem to get the right syntax here.

My XML example related to the strings:

<configuration>
<connectionStrings>
    <add name="db" connectionString="Data Source=(local);Initial Catalog=MyDB;Integrated Security=True" providerName="System.Data.SqlClient" />
    <add name="dbFiles" connectionString="Data Source=(local);Initial Catalog=MyDB;Integrated Security=True" providerName="System.Data.SqlClient" />
    <add name="dbReporting" connectionString="Data Source=(local);Initial Catalog=MyDB;Integrated Security=True" providerName="System.Data.SqlClient" />
    <add name="Logging" connectionString="Data Source=(local);Initial Catalog=MyDB;Integrated Security=True" providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>
3
  • 1
    There's 4 elements with a connectionString attribute in there. Which of them do you want to change, and which new value do you want to assign to them? Commented Jan 8, 2016 at 23:17
  • Well the $dbName variable defines the target this is passed to the function I chose not to include it because the variable shifts with the name values. So if I figure on an answer for 1 I get the answer to change any of them. Commented Jan 8, 2016 at 23:21
  • Further to your last; I am only attempting to replace the "Data Source=(local);Initial Catalog=MyDB;Integrated Security=True" with a string. However, I want to try to do this as an xml object and not just string replace as different configs would make this process difficult. Commented Jan 8, 2016 at 23:26

1 Answer 1

5

If you want to use dot-notation, you could do something like this:

$node = $appConfig.configuration.connectionStrings.add |
        Where-Object {$_.name -eq $dbName}
$node.connectionString = $updatedConnectionString
$appConfig.Save()

or like this (if you don't want to assign the selected node to a variable before modifying its attribute):

($appConfig.configuration.connectionStrings.add | Where-Object {$_.name -eq $dbName}).connectionString = $updatedConnectionString
$appConfig.Save()

Another way would be to use the SelectSingleNode() method with an XPath expression, e.g. like this:

($appConfig.SelectSingleNode("//add[@name='$dbName']").connectionString = $updatedConnectionString
$appConfig.Save()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.