Hi Readers,

We will see in this post on how we can edit web.config or other configuration files using PowerShell. There are already several posts available on internet which shows this functionality, however I faced difficulties in updating connection string in the configuration file as they are not direct. Below steps will help you in updating the connection strings as well.

Sample configuration file is shown below. Other sections of web.config file are not shown in this blog for simplicity.

   1: <configuration>
   2:   <connectionStrings>
   3:     <add name="TestDBEntities" connectionString="metadata=res://*/TestProject.csdl|res://*/TestProject.ssdl|res://*/TestProject.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=SQL01;initial catalog=TestDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
   4:   </connectionStrings>
   5:   <appSettings>
   6:     <add key="SCVMMServerName" value="VMM01" />
   7:     <add key="SCVMMServerPort" value="8100" />
   8:   </appSettings>
   9: </configuration>

We will try to update ‘appSettings’ section and connectionStrings section of this configuration file.

1. Read configuration file in a XML variable

   1: $webConfig = 'C:\inetpub\wwwroot\VMMService\Web.config'
   2: $doc = (Get-Content $webConfig) -as [Xml]

2. Update ‘appSettings’ Section

   1: $obj = $doc.configuration.appSettings.add | where {$_.Key -eq 'SCVMMServerName'}
   2: $obj.value = 'CPVMM02'

3. Update ‘connectionStrings’ section. Here is the tweak, you have to read the root element and then modify the connection string as shown below:-

   1: $root = $doc.get_DocumentElement();
   2: $newCon = $root.connectionStrings.add.connectionString.Replace('data source=SQL01','data source=SQL02');
   3: $root.connectionStrings.add.connectionString = $newCon

4. Save the configuration file

   1: $doc.Save($webConfig)

The combined code will look like below:-

   1: $webConfig = 'C:\inetpub\wwwroot\TestService\Web.config'
   2: $doc = (Get-Content $webConfig) -as [Xml]
   3: $obj = $doc.configuration.appSettings.add | where {$_.Key -eq 'SCVMMServerName'}
   4: $obj.value = 'CPVMM02'
   5:  
   6: $root = $doc.get_DocumentElement();
   7: $newCon = $root.connectionStrings.add.connectionString.Replace('data source=SQL01','data source=SQL02');
   8: $root.connectionStrings.add.connectionString = $newCon
   9:  
  10: $doc.Save($webConfig)

The updated XML will contain modified values as shown below:-

   1: <configuration>
   2:   <connectionStrings>
   3:     <add name="TestDBEntities" connectionString="metadata=res://*/TestProject.csdl|res://*/TestProject.ssdl|res://*/TestProject.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=SQL02;initial catalog=TestDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
   4:   </connectionStrings>
   5:   <appSettings>
   6:     <add key="SCVMMServerName" value="VMM02" />
   7:     <add key="SCVMMServerPort" value="8100" />
   8:   </appSettings>
   9: </configuration>

Let me know if there are some sections of configuration file which you are finding difficult to update and I will add them here in this blog :) Happy Scripting!!!