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="data source=SQL01;initial catalog=TestDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" 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. Add new ‘appSetting’. You will need to create an XmlElement and append it as a child node to ‘appSettings’
$newAppSetting = $doc.CreateElement(“add”)
$doc.configuration.appSettings.AppendChild($newAppSetting)
$newAppSetting.SetAttribute(“key”,”SCVMMIPAdress”);
$newAppSetting.SetAttribute(“value”,”10.10.10.10″);
$doc.configuration.appSettings.AppendChild($newAppSetting)
$newAppSetting.SetAttribute(“key”,”SCVMMIPAdress”);
$newAppSetting.SetAttribute(“value”,”10.10.10.10″);
4. 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
5. Save the configuration file
1: $doc.Save($webConfig)
The combined code will look like below:-
$webConfig = 'C:\inetpub\wwwroot\TestService\Web.config'
$doc = (Get-Content $webConfig) -as [Xml]
$obj = $doc.configuration.appSettings.add | where {$_.Key -eq 'SCVMMServerName'}
$obj.value = 'CPVMM02'
$newAppSetting = $doc.CreateElement("add")
$doc.configuration.appSettings.AppendChild($newAppSetting)
$newAppSetting.SetAttribute("key","SCVMMIPAdress");
$newAppSetting.SetAttribute("value","10.10.10.10");
$root = $doc.get_DocumentElement();
$newCon = $root.connectionStrings.add.connectionString.Replace('data source=SQL01','data source=SQL02');
$root.connectionStrings.add.connectionString = $newCon
$doc.Save($webConfig)
The updated XML will contain modified values as shown below:-
<configuration> <connectionStrings> <add name="TestDBEntities" connectionString="metadata=res://*/TestProject.csdl|res://*/TestProject.ssdl|res://*/TestProject.msl;provider=System.Data.SqlClient;provider connection string="data source=SQL02;initial catalog=TestDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" /> </connectionStrings> <appSettings> <add key="SCVMMServerName" value="CPVMM02" /> <add key="SCVMMServerPort" value="8100" /> <add key="SCVMMIPAdress" value="10.10.10.10" /> </appSettings> </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!!!
That is really help me. Thank you Sonam.
ReplyDeleteThank you!
DeleteOnce I've done step 2 which works fine.. I am finding it difficult to commit these changes back to the file, could you please help me?
ReplyDelete$doc.Save($webConfig)
DeleteThis step helps in saving changes back to file. Please share details of what error you are getting.
This comment has been removed by the author.
ReplyDeleteThis script worked perfectly for my requirement.. thanks a lot Sonam
ReplyDeleteI am glad it helped you :)
ReplyDelete