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)
if($WebApp.WebConfigModifications.Contains($ConfigMod)){
$WebApp.WebConfigModifications.Remove($ConfigMod)
}
$WebApp.WebConfigModifications.Add($ConfigMod)
}
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
$webMod
}
if([Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")-eq $null){throw "Unable to load Microsoft.SharePoint.dll";}
$WebApp=Get-SPWebApplication -Identity $webApplicationUrl
$smtpServerNameKeyName="smtpServerName"
$smtpServerNameValue=''
$sequence=0
$smtpServerPortKeyName="smtpServerPort"
$smtpServerPortValue=''
$sequence=0
$smtpServerNameConfigMod = NewWebConfigModification $smtpServerNameKeyName $sequence $owner $smtpServerNameValue
$smtpServerPortConfigMod = NewWebConfigModification $smtpServerPortKeyName $sequence $owner $smtpServerPortValue
AddConfigModification $smtpServerNameConfigMod $WebApp
AddConfigModification $smtpServerPortConfigMod $WebApp
$WebApp.Update()
$WebApp.Parent.ApplyWebConfigModifications()
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.
References:
Modifying your Web.config file using SPWebConfigModifications
http://gavinb.net/2011/06/13/disable-mobile-redirection-for-a-web-application-via-powershell/




Hey,
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 ?
Thanks