Поместите это в файл с именем 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
}
}
#this is where we start to add mods
$i = 0;
Write-Host ("Adding " + $WebConfigModifications.Count + " webconfig modifications to " + $weburl)
foreach($modEntry in $WebConfigModifications)
{
Write-Host (" + Adding " + $modEntry.Value)
$mod = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification
$mod.Path = $modEntry.Path
$mod.Name = $modEntry.Name
$mod.Sequence = $i++
$mod.Owner = $modEntry.Owner
$mod.Type = $modEntry.Type
$mod.Value = $modEntry.Value
$webApp.WebConfigModifications.Add($mod)
}
$webApp.Update()
$webApp.WebService.ApplyWebConfigModifications()
Затем создайте файл csv с вашими конфигами, например, так:
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'/>
Затем в другом скрипте ps1, в идеале тот, который выиспользуйте для развертывания вашего решения, импортируйте параметры конфигурации csv и вызовите функцию, созданную в первом блоке кода:
#read config mods from CSVs
$mods = Import-CSV .\config\admin-common.webconfigmods.csv
Write-Host "Applying configuration modifications"
#add web config mods to sharepoint using powershell script
&.\AddWebConfigMods "[sharepoint site url]" $mods
&stsadm -o execadmsvcjobs