Wednesday, June 30, 2010

Add Proxy Settings to web.config file of SharePoint with Powershell

I recently needed to modify the default proxy settings in the web.config file of our SharePoint apps. This was done manually to start with, but after many deployments in multiple environments, doing this manually was a chore and often forgotten. I therefore worked on the following PowerShell script that did what we needed.

The following script changes the default proxy settings of:


  <system.net>

    <defaultProxy>

      <proxy autoDetect="true" />

    </defaultProxy>

  </system.net>




to:


  <system.net>

    <defaultProxy useDefaultCredentials="true">

      <proxy usesystemdefault="False" proxyaddress="http://proxy.address" bypassonlocal="True" />

    </defaultProxy>

  </system.net>




Sorry about the crappiness of the cut-and-paste, but this might give you enough help to get you going:


function AddWebConfigModification([Microsoft.SharePoint.Administration.SPWebApplication] $webApp=$(throw 'Parameter -webApp is missing!'),

                                  [string] $name=$(throw 'Parameter -name is missing!'), 

                                  [string] $path=$(throw 'Parameter -path is missing!'), 

                                  [string] $owner=$(throw 'Parameter -owner is missing!'), 

                                  [string] $sequence=0, 

                                  [Microsoft.SharePoint.Administration.SPWebConfigModification+SPWebConfigModificationType] $modificationType=$(throw 'Parameter -modificationType is missing!'), 

                                  [string] $value=$(throw 'Parameter -value is missing!'))

{

    $modification = New-Object -TypeName "Microsoft.SharePoint.Administration.SPWebConfigModification";

    $modification.Name = $name;

    $modification.Path = $path;

    $modification.Owner = $owner;

    $modification.Sequence = $sequence;

    $modification.Type = $modificationType;

    $modification.Value = $value;    

    $webApp.WebConfigModifications.Add($modification);    

}

 

function AddProxySettings([string]$url=$(throw 'Parameter -url is missing!'))

{

    $webApp = GetWebApp($url);    

    $owner = "GUID_HERE";    

    $ensureAttribute = [Microsoft.SharePoint.Administration.SPWebConfigModification+SPWebConfigModificationType]::EnsureAttribute; 

 

    AddWebConfigModification -webApp $webApp -owner $owner -modificationType $ensureAttribute -path "configuration/system.net/defaultProxy" -name "useDefaultCredentials" -value "true";

    AddWebConfigModification -webApp $webApp -owner $owner -modificationType $ensureAttribute -path "configuration/system.net/defaultProxy/proxy" -name "usesystemdefault" -value "False";

    AddWebConfigModification -webApp $webApp -owner $owner -modificationType $ensureAttribute -path "configuration/system.net/defaultProxy/proxy" -name "proxyaddress" -value "http://proxy.address";    

    AddWebConfigModification -webApp $webApp -owner $owner -modificationType $ensureAttribute -path "configuration/system.net/defaultProxy/proxy" -name "bypassonlocal" -value "True";    

    AddWebConfigModification -webApp $webApp -owner $owner -modificationType $ensureAttribute -path "configuration/system.net/defaultProxy/proxy" -name "autoDetect" -value "True";    

 

    [Microsoft.SharePoint.Administration.SPWebService]::ContentService.ApplyWebConfigModifications();

}