I'm trying to change app settings in web.config file using PowerShell
Following is web.config file;
<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=SQL01;initial catalog=TestDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
<appSettings>
<add key="ActivePeriod" value="false" />
<add key="Environment" value="UAT" />
<add key="authmode" value="4" />
<add key="IsEncryptedConfig" value="true" />
<add key="LogErrorsToText" value="true" />
</appSettings>
</configuration>
I'm want to the change the app settings values. For that I have stored all the corresponding values in PowerShell dictionary. Here is my dictionary looks like;
Key Value
----- -----
ActivePeriod true
Environment prod
LogErrorsToText false
Now, I want to match each of the Dictionary key's with the appsetting key's. if any of the dictionary key matches with appsetting key, it should replace the corresponding values. In my case, I'm expecting following output;
<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=SQL01;initial catalog=TestDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
<appSettings>
<add key="ActivePeriod" value="true" />
<add key="Environment" value="prod" />
<add key="authmode" value="4" />
<add key="IsEncryptedConfig" value="true" />
<add key="LogErrorsToText" value="false" />
</appSettings>
</configuration>
Can someone please suggest me possible solutions. Thanks in advance.
Iterate over the Keys
in the dictionary, and find the corresponding <add />
node in the Xml document, then set the attribute if found:
$xml = [xml](Get-Content .\app.config)
$Dictionary = @{
ActivePeriod = 'true'
Environment = 'prod'
LogErrorsToText = 'false'
}
foreach($key in $Dictionary.Keys)
{
Write-Host "Locating key: '$key' in XML"
# Use XPath to find the appropriate node
if(($addKey = $xml.SelectSingleNode("//appSettings/add[@key = '$key']")))
{
Write-Host "Found key: '$key' in XML, updating value to $($Dictionary[$key])"
$addKey.SetAttribute('value',$Dictionary[$key])
}
}
The answer of Mathias R. Jenssen provides a good overview on how to do things, but as mentioned by the OP in the comments of his/her answer, it does not work out of the box (for later versions of PowerShell at least, I tested on PS 5.1).
What worked for me:
$xml = [xml](Get-Content '.\app.config')
$Dictionary = @{
ActivePeriod = 'true';
Environment = 'prod';
LogErrorsToText = 'false';
}
foreach($key in $Dictionary.Keys)
{
Write-Host "Locating key: '$key' in XML"
# Use XPath to find the appropriate node
if(($addKey = $xml.SelectSingleNode("//appSettings/add[@key = '$key']")))
{
Write-Host "Found key: '$key' in XML, updating value to $($Dictionary[$key])"
$addKey.SetAttribute('value',$Dictionary[$key])
}
}
$xml.Save('.\app.config')
Notice that the difference lies in the semicolumns of the Hash Table declaration. I did this change according to the official Microsoft documentation.
Also, as requested by the OP in the comments, the last line shows how to save the xml file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With