How to change values ​​of web.config file using powershell

Hi, I'm trying to change the connection string values ​​for the web.config file, but get an error:

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

Here's the script I use:

 $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 changed the code as below, but it still does not work, and I get 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) 
+5
source share
3 answers

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 
+3
source

Late, but just in case someone can find some use for this, I wrote a reusable Power-Shell for this.

  #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) 
+2
source

the dirty way could be to replace the text without parsing xml, something like this (in case the previous connection restrictions were not previously 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 
+1
source

Source: https://habr.com/ru/post/1200915/


All Articles