Reputation: 45
Below is the content of source/destine XML(Web.Config) file, in which I have to replace value of an attribute. This needs to be done for several config files, so need your help to get this done using powershell. I did try with the codes for replacing strings which are available in stackOverFlow- but dint make it..
<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<connectionStrings>
<add name="PrcEntities"
providerName="System.Data.SqlClient"
connectionString="Server=DDD05DB01,63518;Database=BBBDB001;Trusted_Connection=true;multipleactiveresultsets=true;Pooling=false"/>
<add name="CoreItemContext"
providerName="System.Data.SqlClient"
connectionString="Server=DDD15DB03,63520;Database=BBBDB002;Trusted_Connection=true;multipleactiveresultsets=true;Pooling=true"/>
</connectionStrings>
..
..
....
</configuration>
This is my Powershell code to query the attribute named "ConnectionString" from above XML file
$Path = "C:\Ps\Web.config"
$con="connectionString"
[xml]$Types = Get-Content $Path
Select-Xml -Xml $Types -XPath "//add" | Select-Object -ExpandProperty Node|Select-Object name,$con | Format-List
Which will result in :
name : PrcEntities connectionString : Server=DDD05DB01,63518;Database=BBBDB001;Trusted_Connection=true; multipleactiveresultsets=true;Pooling=false
name : CoreItemContext connectionString : Server=DDD15DB03,63520;Database=BBBDB002;Trusted_Connection=true; multipleactiveresultsets=true;Pooling=true
Now, I want to replace the value of attribute 'connectionString' with details like: Server=DBD05DB01,63518;Database=DDBDB001;Trusted_Connection=true;multipleactiveresultsets=true;Pooling=false in both the places(PrcEntities & CoreItemContext)
This change should be saved to same source file. Please help me on this!!
Upvotes: 3
Views: 7953
Reputation: 126902
$path = "D:\Web.Config"
[xml]$xml = Get-Content $path
$xml.configuration.connectionStrings.add | Foreach-Object {
$_.connectionString = 'Server=DBD05DB01,63518;Database=DDBDB001;Trusted_Connection=true;multipleactiveresultsets=true;Pooling=false'
}
$xml.Save($path)
Upvotes: 7