SharePoint - Automating web.config Changes for Proxy Server Settings

One of my customers recently needed to provide the ability for end users to access external RSS feeds from SharePoint, this requires that all SharePoint Web Servers have access to the Internet. In this specific case my customer controlled access to the Internet via a proxy server therefore the easiest way to set this up was make some modifications to the web.config for the Web Applications that needed the ability to connect to external RSS feeds as per - https://msdn.microsoft.com/en-us/library/kd3cf2ex(v=vs.85).aspx.

Rather than making nasty manual changes to the web.config files on each Web server we decided to apply the changes correctly using the Microsoft.SharePoint.Administration.SPWebConfigModification class, which takes care of rolling out these changes to all Web Servers. I put together the PowerShell below to automate the addition of these settings rather than deploying via a solution (which seemed a little overkill).

One thing worth pointing out is that the account that the Application Pool for the Web Application that you configure to use a Proxy will require permissions to use the Proxy.

Simply update the highlighted values and execute, this was tested on SharePoint 2010 but should also work with SharePoint 2013 (and possibly MOSS 2007!):

Add Proxy Settings

asnp *SharePoint* -ea 0
#Bind to Web App
$WebApp = Get-SPWebApplication https://intranet.contoso.com
#Configure Auth Setting
$ProxyCreds = New-Object -TypeName "Microsoft.SharePoint.Administration.SPWebConfigModification" -ArgumentList "useDefaultCredentials", "configuration/system.net/defaultProxy"
$ProxyCreds.Sequence = 0
$ProxyCreds.Owner = "Proxy for RSS Feeds"
$ProxyCreds.Type = "1"
$ProxyCreds.Value = "True"
$WebApp.WebConfigModifications.Add($ProxyCreds)
#Add Proxy Section and Required Settings
$Proxy = New-Object -TypeName "Microsoft.SharePoint.Administration.SPWebConfigModification" -ArgumentList "proxy", "configuration/system.net/defaultProxy"
$Proxy.Sequence = 0
$Proxy.Owner = "Proxy for RSS Feeds"
$Proxy.Type = "0"
$Proxy.Value = "<proxy usesystemdefault='false' proxyaddress='https://proxy:8080' bypassonlocal='true'></proxy>"
$WebApp.WebConfigModifications.Add($Proxy)
$WebApp.Update()
$WebApp.Parent.ApplyWebConfigModifications()

Remove Proxy Settings

$ProxySettings = $Webapp.WebConfigModifications | Where {$_.Owner -eq "Proxy for RSS Feeds"}
Foreach ($ProxySetting in $ProxySettings)
{
$WebApp.WebConfigModifications.Remove($ProxySetting)
$WebApp.Update()
$WebApp.Parent.ApplyWebConfigModifications()
}

Brendan Griffin - @brendankarl