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