Partager via


Automating config file changes : Part 2 – modifying already existing config keys

This post contains the Powershell script to modify already existing config key values in a config file.

A sample web.config file is given below

<?xml version="1.0"?> <configuration>   <system.web>     <customErrors mode="On" defaultRedirect="Error.htm">       <error statusCode="404" redirect="https://myAppDevWeb/404.aspx"/>     </customErrors>   </system.web>   <appSettings>     <add key="DBServer" value="myAppDevDB"/>   </appSettings> </configuration>

We want to modify the 404 redirect page address and DBServer by passing the required values to the Powershell script.

The following Powershell script helps us achieve this. We will name this script webUpdate.ps1 and store it in C:\Scripts

# In Powershell, variable is declared by prefixing dollar symbol $ # Anything followed by hash # is treating as a single line comment # –eq is the comparison operator # Plus + is used to concatenate strings

# Parameter declaration which will be passed to the script during execution

Param (     $webConfigPath,        # Path to the web.config file. Make sure file is not read-only     $webServerName,      # Name of the Webserver. This value will be used to construct the 404 page address     $dbServerName         # Name of the DB Server. )

$xml = [xml](get-content $webConfigPath);                           # Create the XML Object and open the web.config file $root = $xml.get_DocumentElement();                                  # Get the root element of the file.

foreach( $item in $root."system.web".customErrors.error)         # Iterate through each error node under the customErrors tag {     if( $item.statusCode -eq "404" )                                       # Check if the current statusCode is 404         { $item.redirect = “https://” + $webServername + "/404.aspx"; }         # Setting the redirect value for 404 error page }

foreach( $item in $root.appSettings.add)                              # Updating the DBServer Name {         if( $item.key -eq "DBServer" )             { $item.value = $dbServerName; } }

$xml.Save($webConfigPath)                                               # Finally, Saving the file

We will now run the script on a web.config file present at the location C:\MyApplication\web.config
The requirement is to update the config values for the Test environment where the webserver name is myAppTestWeb and the DB Server name is myAppTestDB

Open Command Prompt

c:\>Powershell

PS C:\> cd scripts

PS C:\scripts> .\webUpdate.ps1 “C:\MyApplication\web.config” myAppTestWeb myAppTestDB

 

 

 

Related Post: Automating config file changes : Part 1 – Installing Microsoft Windows Powershell

Comments

  • Anonymous
    May 28, 2009
    This post talks about removing an existing XML element from the config file. Consider a scenario where

  • Anonymous
    May 30, 2009
    After updating and deleting element from an XML config file, lets see how to add new elements at the