7

i want to modify my web config file using powershell . i stuck in somewhere . i want to update appsettings and also connectionsstring information at the same time when i change them in powershel

I have this code but it changes only apppsettings value when i change it here and run it but i also want to include connectionstring here. How can i achieve it ?

$webConfig = "C:\Inetpub\Wwwroot\application\web.config"
$doc = new-object System.Xml.XmlDocument
$doc.Load($webConfig)
$doc.get_DocumentElement()."appsetting".add[0].value = "true"
$doc.Save($webConfig)

Here is my web config file

 <appSettings>
     <add key="mykey1" value="false"/>
     <add key="mykey2" value="true"/>
     <add key="mykey3" value="false"/>
</appSettings>

  <connectionstrings>

  <add name="myname1" connectinstring="Data Source=ABDULLAH-PC\SQLEXPRESS;Initial Catalog=UserDataBase;
  Integrated Security=True" providerName="System.Data.SqlClient" />
  <add name="myname2" connectinstring="myconnectionstring2" />
   <add name="myname3" connectinstring="myconnectionstring3" />
 </connectionStrings>

Here i want to upadate appsettings -( key and value) and also connectionstrings( name and initialcatalog) at the same time

when i tried your code it gives me this error

Property '#text' cannot be found on this object; make sure it exists and is settable.
At line:3 char:66
+ $doc.SelectSingleNode('//appSettings/add[@key="mykey1"]/@value'). <<<< '#text' = 'false'
+ CategoryInfo          : InvalidOperation: (#text:String) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound

Property '#text' cannot be found on this object; make sure it exists and is settable.
At line:4 char:85
+ $doc.SelectSingleNode('//connectionStrings/add[@name="myname1"]/@connectionstring'). <<<< '#text'='my_string'       
  + CategoryInfo          : InvalidOperation: (#text:String) [], RuntimeException
   + FullyQualifiedErrorId : PropertyNotFound
0

1 Answer 1

23
$webConfig = "C:\Inetpub\Wwwroot\application\web.config"
$doc = (gc $webConfig) -as [xml]
$doc.SelectSingleNode('//appSettings/add[@key="mykey1"]/@value').'#text' = 'true'
$doc.SelectSingleNode('//connectionStrings/add[@name="myname1"]/@connectionstring').'#text' = 'my_string'
$doc.Save($webConfig)

You can use XPath to select your nodes and set their value via the #text property PowerShell adds.

Note - your example xml has problems with casing and some typos. Here is what I tested with:

<root>
    <appSettings>
         <add key="mykey1" value="false"/>
    </appSettings>
    <connectionStrings>
        <add name="myname1" connectionstring="Data Source=ABDULLAH-PC\SQLEXPRESS;Initial Catalog=UserDataBase; Integrated Security=True" providerName="System.Data.SqlClient" />
    </connectionStrings>
</root>
Sign up to request clarification or add additional context in comments.

4 Comments

Hi , what should i write in #text area ?
@user2375896 that's actually supposed to be #text. PowerShell puts the value of the XML node in that property. Also i'm guessing at your XML layout. If you post the XML you have and what you want added and where I can make the example better for you.
I'm using powershell version 4 and the '#text' did not work for me. Replacing '#text' with .value seems to do the trick.
I'm also using PowerShell version 4. But Replacing '#text' with .value is not working. can you post me a working example. Thanks in advance.

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.