14

Hi I am trying to change the values of a connection string for a web.config file but getting error:

The property 'connectionString' cannot be found on this object. Verify that the property exists and can be set.

Here's the script I'm using:

$webConfig = 'C:\Users\test\Desktop\web\web.config'
$doc = (Get-Content $webConfig) -as [Xml]
$obj = $doc.configuration.appSettings.add | where {$_.Key -eq 'CommandTimeOut'}
$obj.value = '60'
$config = [xml](gc $webConfig)  
$con= $config.configuration.connectionStrings.add|where-object{$_.name -eq "password"};

$con.connectionString = $con.connectionString -replace "123456", "admin1234"

$doc.Save($webConfig)

I've modified the code as below, but it's still not working and I'm getting the same error.

$cfg = [xml](gc $webConfig) 
$con= $cfg.configuration.connectionStrings.add|where-object{$_.name -eq "password"};
$cfg.configuration.connectionStrings.add.connectionString=   
$cfg.configuration.connectionStrings.add.connectionString -replace "123456","admin123"
$doc.Save($webConfig)
2
  • can you share the web.config ? at least the <connectionStrings> part ? Commented Aug 22, 2014 at 10:07
  • <add name="membership" connectionString="Data Source=server1;Initial Catalog=inventory;MultipleActiveResultSets=true;user id=inventoryWebUser;password=123456" providerName="System.Data.SqlClient" /> Commented Aug 22, 2014 at 10:40

3 Answers 3

17

Late, but just in case someone may find a use for it, I've written a re-usable Power-Shell do to that.

    #set the value of this to your own db config
$myConnectionString = "Data Source=.;Initial Catalog=<Database>;Integrated Security=True";

$webConfig = '.\Api\Web.config'
$dbUpConfig = '.\Database\App.config'
$unitTestConfig = '.\Test\App.config'

Function updateConfig($config) 
{ 
$doc = (Get-Content $config) -as [Xml]
$root = $doc.get_DocumentElement();
$activeConnection = $root.connectionStrings.SelectNodes("add");
$activeConnection.SetAttribute("connectionString", $myConnectionString);
$doc.Save($config)
} 

updateConfig($webConfig)
updateConfig($dbUpConfig)
updateConfig($unitTestConfig)
Sign up to request clarification or add additional context in comments.

Comments

4

here is a step-by-step example :

[xml]$x='<connectionStrings>
<add name="membership" connectionString="Data Source=server1;Initial Catalog=inventory;MultipleActiveResultSets=true;user id=inventoryWebUser;password=123456" providerName="System.Data.SqlClient" />
<add name="test" connectionString="Data Source=server1;Initial Catalog=inventory;MultipleActiveResultSets=true;user id=inventoryWebUser;password=123456" providerName="System.Data.SqlClient" />
</connectionStrings>'

$mycon=$x.connectionStrings.add |?{$_.name -eq "membership"}
$mycon.connectionString=$mycon.connectionstring -replace "password=123456","password=admin123"
$x.save("c:\temp\newconf.xml")
gc c:\temp\newconf.xml

1 Comment

For a web.config file, start with $cfg = (Get-Content "C:\inetpub\wwwroot\mysite\web.config") -as [Xml] , then $x would be $cfg.configuration, i.e. $cfg.configuration.connectionStrings.add...
2

a dirty way could be to replace the text without parsing the xml, something like this (in case there is no previous connectionstrings defined ) :

$replacementstring=@"
<connectionStrings>
  <add 
    name="NorthwindConnectionString" 
    connectionString="Data Source=serverName;Initial 
    Catalog=Northwind;Persist Security Info=True;User 
    ID=userName;Password=password"
    providerName="System.Data.SqlClient"
  />
</connectionStrings>
"@

(gc c:\temp\web.config) -replace "<connectionStrings/>" ,$repl  | out-file c:\temp\new_web.config

7 Comments

i just added the connection string. i am not sure what am i missing in my script which is not replacing the connection string value. however it does change the "key" values according to the script
this worked for me : $x.configuration.connectionStrings.add.connectionString= $x.configuration.connectionStrings.add.connectionString -replace "123456","admin123"
still getting the same error, i just modified in question. Am i using the code and its placement the right way???
Your where condition is wrong it shoud be ` where-object{$_.name -eq "membership"}` you are also mixing $cfg and $con
if you look at my script (modified script in question) is it good to go if i replace password with membership
|

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.