如何使用powershell在web.config中的<configsections>中添加新的<section>

时间:2016-11-03 15:05:17

标签: powershell sharepoint web-config

我想在configSections标签

中添加新的section元素
<configuration>
<configSections>
<sectionGroup name="SharePoint">
  <section name="SafeControls" type="Microsoft.SharePoint.ApplicationRuntime.SafeControlsConfigurationHandler, Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
  <section name="RuntimeFilter" type="System.Configuration.SingleTagSectionHandler, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  <section name="WebPartLimits" type="System.Configuration.SingleTagSectionHandler, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />      
</sectionGroup>

<section name="MySection" type="MyType" />
</configSections>

我如何添加&#34; MySection&#34;在configSections内但在sectionGroup外面?

由于

1 个答案:

答案 0 :(得分:0)

我得到了解决方案。

$path = $("IIS:\Sites\" + $title)
$physicalPath = $(get-item "$path").physicalPath
$configpath = $($physicalPath + "\web.config");
$content = Get-Content -Path $configpath
[System.Xml.XmlDocument] $xmlDoc = new-object System.Xml.XmlDocument
$xmlDoc.LoadXml($content)
$oauthName = "oauth2.login.configuration"
$oauthSectionNode = $xmlDoc.selectSingleNode("/configuration/configSections/section[@name='$oauthName']")

if(!$oauthSectionNode)
{
    $mainNode = $xmlDoc.selectSingleNode("/configuration/configSections")
    $oauthSectionNode = $xmlDoc.CreateNode("element","section","")

    $oauthAttr = $xmlDoc.CreateAttribute("name")
    $oauthAttr.Value = "MySection"
    $oauthSectionNode.Attributes.Append($oauthAttr)

    $oauthTypeAttr = $xmlDoc.CreateAttribute("type")
    $oauthTypeAttr.Value = "MyType"
    $oauthSectionNode.Attributes.Append($oauthTypeAttr)

    $mainNode.AppendChild($oauthSectionNode)
    $xmlDoc.Save($configpath) 
}