SharePoint and Web.config modifications

Ever since MOSS 2007 we all know the Web.Config Modifications Manager within the SharePoint API. But today I stumbled upon this PowerShell commandlet: Add-WebConfiguration. This commandlet is from the IIS7(.5) powershell module WebAdministration.

The short version is, you can use this in you deploy script to do Web.Config modifications

Example for an AppSettings Configuration Key:”

Add-WebConfiguration /appSettings "IIS:\sites\Default Web Site" -atIndex 0 -Value @{key="DBConnectionString";value="server=10.2.34.1"}

This will result in the following addition of your Web.Config.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appSettings>
        <add key="DBConnectionString" value="server=10.2.34.1" />
    </appSettings>
</configuration>

Note: Be carefull with ANY Web.Config modification, you can break stuff!
Load the WebAdministration module in powershell:

#load webadministration powershell module
$hasSnapin = get-pssnapin | Select { $_.Name.toLower().Trim() = "webadministration" }
if ($hasSnapin -ne $null) {
  # IIS7 / Windows 2008
  add-pssnapin WebAdministration
} else {
  # IIS7.5 / Windows 2008 R2
  import-module WebAdministration
}
Advertisements

3 Responses to “SharePoint and Web.config modifications”

  1. Mike Says:

    Thank you for your post. It is the most useful thing I have found regarding using PowerShell to edit the web.config file.

    Unfortunately, when I run this:

    Add-WebConfiguration /system.web/membership/providers “IIS:\sites\[site name]” -value @{name=”FBAMembershipProvider”;`
    type=”System.Web.Security.SqlMembershipProvider, System.Web, Version= 2.0.0.0, Culture= neutral, PublicKeyToken= b03f5f7f11d50a3a”;`
    connectionStringName=”FBAconn”;`
    applicationName=”/”}

    it adds the new provider element, but it only has a name and a type, it does not have the connectionStringName or the applicationName attributes.

    Do you have any suggestions?

  2. Koen van der Linden Says:

    Looks good, but I think this only updates the current Web Application that’s running on the box. What about the web.configs on the other Web Front End servers in the SharePoint farm.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: