Put this in a file called AddWebConfigMods.ps1
Param ($weburl, $WebConfigModifications ) $url = New-Object System.Uri($webUrl) [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") | Out-Null $webApp = [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup($url) #delete mods by the owners being added $ownerstodelete = $WebConfigModifications | Group-Object owner | Select-Object Name foreach($owner in $ownerstodelete) { $modstodelete = @() foreach($mod in $webApp.WebConfigModifications) { if($mod.Owner -eq $owner.Name) { $modstodelete += $mod } } Write-Host ("Deleting " + $modstodelete.Count + " mods for owner: " + $owner) foreach($delmod in $modstodelete) { Write-Host (" + Deleting " + $delmod.Value) $webApp.WebConfigModifications.Remove($delmod) | Out-Null } }
Then create a csv file with your configurations, for example:
Name,Path,Owner,Type,Value system.serviceModel,configuration,alinean-common,EnsureSection,<system.serviceModel/> connectionStrings,configuration,alinean-common,EnsureSection,<connectionStrings /> appSettings,configuration,alinean-common,EnsureSection,<appSettings /> serviceHostingEnvironment,configuration/system.serviceModel,alinean-common,EnsureChildNode,<serviceHostingEnvironment aspNetCompatibilityEnabled='true'/>
Then, in another ps1 script, ideally used to deploy the solution, import the csv configuration parameters and call the function created in the first block of code:
#read config mods from CSVs $mods = Import-CSV .\config\admin-common.webconfigmods.csv Write-Host "Applying configuration modifications"
source share