Sharepoint Web Config Modification Using PowerShell

Below is an example of how to add a web configuration modification in SharePoint Using PowerShell.  The script has a pience of function to add the Location configuration for timeout as well as a function that allows you to delete the modication done by the user.  Rew points to consider when you want to write that code.

Owner: Every configuration is tagged with the owner and can be retreived using the propertly.  Be careful in making different web config entries as if you set a single Owner all the configuration can get cleaned up.

$configMod.Name = “add[@name=””location””]” : Define the Name of entry you did in value.
$configMod.Path = “/configuration” : Define the place you want to add the entry.  You can extended it with /Configuration/SharePoint/SafeControls/
$configMod.Value = “<location path=’_vti_bin/sitedata.asmx’></location> : Actual Full String.  use Double (Double Quotes “”) to cover string and make sure you are using the right double quotes “” by pasting the script in notepad or PowerShell ISE.

Write-Host "===============================================" -ForegroundColor Yellow
Write-Host "Adding Timout Value to SharePoint Web.Config" -ForegroundColor Yellow
Write-Host "===============================================" -ForegroundColor Yellow

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

function RemoveMyEntries($webapp, $Owner)
{
   $oldMods = @();
   $webapp.WebConfigModifications | where-object { $_.Owner -eq $Owner } | foreach-object { $oldMods = $oldMods + $_}
   Write-Host $oldMods.Count "Items Found"
   $oldMods | foreach-object { $webapp.WebConfigModifications.Remove($_) }
   
   #$webapp.Update()
   #$webapp.Parent.ApplyWebConfigModifications()
}

$Owner = "Jerry"
$WebApp = Get-SPWebApplication http://portal.hp.com

Write-Host $WebApp
#Write-Host "Removing All Entries Made by $Owner"
RemoveMyEntries $WebApp $Owner 
sleep -Seconds 10
#Write-Host "Removed All Entries Made by $Owner"

Write-Host "Creating New Entry"
$configMod = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification
$configMod.Name = "add[@name=""location""]"
$configMod.Path = “/configuration”
$configMod.Value = "<location path='_vti_bin/sitedata.asmx'><system.web><httpRuntime executionTimeout='3600'/></system.web></location>"
$configMod.Sequence = 0
$configMod.Type = 0 #for enum value of SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode
$configMod.Owner = $Owner
$WebApp.WebConfigModifications.Add($configMod)
$WebApp.Update()
$WebApp.Parent.ApplyWebConfigModifications()
Write-Host "Web Application Entry has been added. Wait for a couple of minutes and then test it out" -ForegroundColor Green
Advertisements

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