2016-11-03 1 views
-1

Я хочу, чтобы добавить новый раздел элемент внутри тега configSectionsКак добавить новый <section> внутри <configSections> в web.config с помощью PowerShell

<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> 

Как я могу добавить «секции MySection» внутри configSections но за пределами sectionGroup?

Thanks

ответ

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) 
}