Partager via


Using MSDeploy to update and remove sections in web.config. A simple example

I got a case the other day where customer was using Microsoft Web Deploy 3.0.

"Web Deploy 3.0"

https://www.iis.net/downloads/microsoft/web-deploy

 

There were 3 questions around this.

. How to change the value of some settings for their site in the web.config.

. How to remove or delete a whole section in the web.config.

. How to do this using the -setParamFile argument.

This post shows how to do this in a step by step fashion. The assumption is of course that Web Deploy is installed.

This should be at: C:\Program Files\IIS\Microsoft Web Deploy V3\

Rather than using two IIS instances, I will just use two directories. One to be the Source and one to be the Destination.

#1 Create a Src directory: C:\WebDeployDemo\Src

#2 Create a Dst directory: C:\WebDeployDemo\Dst

#3 Create a web.config in both Src and Dst directories, i.e.:

C:\WebDeployDemo\Src\web.config

  and

C:\WebDeployDemo\Dst\web.config

#4 Set the content of both the web.config files to be:

<configuration>

            <sites>

                         <site description="someDescription" siteId="someId">

                                     <siteSettings aBooleanSetting="False" aStringSetting="someStringValue"/>

                         </site>

            </sites>

            <customSection>

                         <add value="someValue1" enabled="False"/>

                         <add value="someValue2" enabled="True"/>

            </customSection>

</configuration>

#5 Create a parameter file (to be used by the -setParamFile argument) called ParameterFile.xml in the C:\WebDeployDemo directory, i.e.:

C:\WebDeployDemo\ParameterFile.xml

Set the content to:

<parameters>

            <parameter name="changeBool" value="True">

                         <parameterEntry type="XMLFile" scope="web.config$" match="//configuration/sites/site/siteSettings/@aBooleanSetting"/>

            </parameter>

            <parameter name="changeString" value="aNewAndChangedValue">

                         <parameterEntry type="XMLFile" scope="web.config$" match="//configuration/sites/site/siteSettings/@aStringSetting"/>

            </parameter>

            <parameter name="removeSection" value="">

                         <parameterValidation kind="AllowEmpty" />

                         <parameterEntry type="XMLFile" scope="web.config$" match="//configuration/customSection"/>

            </parameter>

</parameters>

The above file will do the following:

. Change the “aBooleanSetting” to TRUE in the destination.

. Change the “aStringSetting” to “aNewAndChangedValue” in the destination.

. Remove the “customSection” section all together in the destination.

#6 Open a command prompt and navigate to C:\Program Files\IIS\Microsoft Web Deploy V3\

#7 Run the following command to execute the changes using the parameters file we just created:

msdeploy -verb:sync -source:dirpath=C:\WebDeployDemo\Src -dest:dirpath=C:\WebDeployDemo\Dst -setParamFile=C:\WebDeployDemo\ParameterFile.xml

#8 Check the web.config in the Dst folder, it should now be:

<configuration>

            <sites>

                         <site description="someDescription" siteId="someId">

                                     <siteSettings aBooleanSetting="True" aStringSetting="aNewAndChangedValue" />

                         </site>

            </sites>

</configuration>

And there you go :-)

NOTE: The above parameter in the ParameterFile.xml will remove the <customSection> section all together.

If you would like to keep the section and but remove all entries, in other words to have the web.config look as follows:

<configuration>

            <sites>

                         <site description="someDescription" siteId="someId">

                                     <siteSettings aBooleanSetting="True" aStringSetting="aNewAndChangedValue" />

                         </site>

            </sites>

            <customSection>

                        

            </customSection>

</configuration>

Then you can use a wildcard in the ParameterFile.xml like so:

            <parameter name="removeSection" value="">

                         <parameterValidation kind="AllowEmpty" />

                         <parameterEntry type="XMLFile" scope="web.config$" match="//configuration/customSection//*"/>

            </parameter>

Some references:

"Web Deploy Operation Settings"

https://technet.microsoft.com/en-us/library/dd569089(v=WS.10).aspx

"Using declareParam and setParam"

https://technet.microsoft.com/en-us/library/dd569084(v=ws.10).aspx

Comments

  • Anonymous
    November 06, 2012
    Hi Michael, I have a web deployment package which contains the below files. WebApplication.deploy.cmd WebApplication.SetParameters.xml WebApplication.SourceManifest.xml WebApplication.zip I am using octopus tool to deploy the web application in multiple servers. Problem: Can i pass configurable parameters from octopus (i mean parameters for web.config to update some keys) to Deployment package to WebApplication.deploy.cmd. If yes, How can i pass the parameters to update my web.config file. Any help in this regard would be great. Thanks, Sabari.

  • Anonymous
    November 03, 2013
    FYI, the "type" attribute on "parameterEntry" should be "kind".

  • Anonymous
    November 21, 2013
    Hi, Thanks for this, It works really well. One question I have is regarding that custom section. I want to update a web.config file, however I want to keep a section of the web.config on the destination, but replace everything else. For example I have a section in my web.config as below.  <appSettings>    <add key="LookupCacheExpiration" value=""/>    <add key="MainPrimaryColour" value=""/>    <add key="MainBackgroundColour" value=""/>    <add key="MainWindowColour" value=""/>    <add key="MainWindowBorderBrushColour" value=""/>    <add key="CacheUser" value=""/>    <add key="CacheHash" value=""/>    <add key="SessionServer" value=""/>    <add key="ApplicationServer" value=""/>    <add key="LogoFileName" value=""/>    <add key="LogoAlignment" value=""/>    <add key="CrystalViewer" value=""/>    <add key="ExclusionsOnline" value=""/>  </appSettings> I don't want to overwrite this section on my destination, I want it to keep the values it has, but sometimes the rest of the web.config file changes. Is there a way for me to use a wildcard to exclude everything between appSettings, but replace everything else in the web.config with the source file? Many thanks Carl