Home > PowerShell, SharePoint 2010 > How to add configuration settings using SPWebConfigModification and PowerShell Script

How to add configuration settings using SPWebConfigModification and PowerShell Script

In recent project, I need to store SMTP configurations in the web.config. In this post I’d like to show you how to update the web.config using SPWebConfigModification and PowerShell Script.

#add configuration settings using SPWebConfigModification and PowerShell Script
#param( [Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)]
#			[Microsoft.SharePoint.PowerShell.SPWebApplicationPipeBind]
#			$WebApplication)
param( [string]$webApplicationUrl,[string]$smtpServerName,[string]$smtpServerPort,[string]$owner)
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

function AddConfigModification
{    param ([Microsoft.SharePoint.Administration.SPWebConfigModification]$ConfigMod,$WebApp)

function NewWebConfigModification
	param ([string]$keyName,$sequence,[string]$owner,[string]$value)
    $webMod =  New-Object Microsoft.SharePoint.Administration.SPWebConfigModification
    $webMod.Path = "configuration/appSettings"
    $webMod.Name = [string]::Format("add[@key='{0}']", $keyName)
    $webMod.Sequence = $sequence
    ## SPWebConfigModificationType.EnsureChildNode -> 0
	## SPWebConfigModificationType.EnsureAttribute -> 1
	## SPWebConfigModificationType.EnsureSection -> 2
    $webMod.Type = 0
    $webMod.Owner = $owner;
    $webMod.Value = $value

if([Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")-eq $null){throw "Unable to load Microsoft.SharePoint.dll";}
$WebApp=Get-SPWebApplication -Identity $webApplicationUrl
$smtpServerNameConfigMod = NewWebConfigModification $smtpServerNameKeyName $sequence $owner  $smtpServerNameValue
$smtpServerPortConfigMod = NewWebConfigModification $smtpServerPortKeyName $sequence $owner  $smtpServerPortValue
AddConfigModification $smtpServerNameConfigMod $WebApp
AddConfigModification $smtpServerPortConfigMod $WebApp

You can download the powershell code here.If you run the code and experience the following exception , you need to recreate your web application , it should solve the problem.

Exception calling “ApplyWebConfigModifications” with “0” argument(s): “Object reference not set to an instance of an object.”

*********Update 8 May 2012********

I have updated my code samples so that it will read from a XML for the configuration setting.You can download it from here.


Modifying your Web.config file using SPWebConfigModifications


How to Create an Object in PowerShell

  1. Thoma
    June 22, 2012 at 1:50 am

    Thanks for the script.
    I have a question though. I saw that to use smtp configuration in your web.config you had to use these nodes :

    Is it the same as what you’re doing ?


  1. July 17, 2012 at 11:37 am

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: