Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update app settings in web.config with the help of PowerShell dictionary

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=&quot;data source=SQL01;initial catalog=TestDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" 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=&quot;data source=SQL01;initial catalog=TestDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" 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.

like image 280
mahesh Avatar asked Jul 20 '16 13:07

mahesh


2 Answers

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])
    }
}
like image 154
Mathias R. Jessen Avatar answered Oct 28 '22 12:10

Mathias R. Jessen


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.

like image 1
Gustavo Avatar answered Oct 28 '22 11:10

Gustavo