从Powershell插入具有多个属性的元素

时间:2017-02-27 17:33:06

标签: xml powershell

我想将此行添加到system.web标记

下的xml文件中
if length > 1

XML文件:

<machineKey decryption="AES" validation="SHA1" />

能够使用以下步骤添加单个属性:

<configuration>
    <system.web>
        <httpRuntime maxRequestLength="1024000" executionTimeout="600" requestValidationMode="2.0" />
        <pages smartNavigation="false" validateRequest="true" controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID" />
        <compilation defaultLanguage="vb" debug="false" targetFramework="4.5" />
        <customErrors defaultRedirect="~/Error/Redirect.aspx" mode="On" redirectMode="ResponseRewrite" />
        <authorization>
          <allow users="" />
        </authorization>
        <trace enabled="false" requestLimit="25" pageOutput="true" traceMode="SortByTime" localOnly="true" />
        <sessionState mode="InProc" stateConnectionString="tcpip=127.0.0.1:42424" sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes" cookieless="false" timeout="30" />
        <globalization requestEncoding="utf-8" responseEncoding="utf-8" />
        <httpHandlers>
          <add verb="*" path="Compare.aspx" type="kCura.EDDS.WebAPI.Compare, kCura.EDDS.WebAPI" />
        </httpHandlers>
        <webServices>
          <protocols>
            <add name="HttpPost" />
            <remove name="HttpGet" />
            <remove name="Documentation" />
          </protocols>
        </webServices>
      </system.web>
</configuration>

2 个答案:

答案 0 :(得分:1)

您需要使用SetAttribute()两次,每个属性一次。

if($xmldoc.configuration.'system.web'.SelectSingleNode("machineKey") -eq $null) {
    $ChildElement = $xmlDoc.CreateElement('machineKey');
    $ChildElement.SetAttribute('decryption','AES')
    $ChildElement.SetAttribute('validation','SHA1')
    $xmlDoc.configuration.'system.web'.InsertBefore($ChildElement,$xmlDoc.configuration.'system.web'.authentication)
}

$xmlDoc.Save($fileName)

添加此节点:

<machineKey decryption="AES" validation="SHA1" />

答案 1 :(得分:0)

我不确定是什么阻止你不止一次致电SetAttribute()

$ChildElement.SetAttribute('decryption','False')
$ChildElement.SetAttribute('validation','SHA1')
相关问题