2017-02-27 27 views
-2

多個屬性的元素,我想這行添加到我的XML文件下的System.Web標籤插入與Powershell的

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

XML文件:

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

之所以能夠添加使用這些步驟的單個屬性:

$ChildElement = $xmlDoc.CreateElement('machineKey'); 
$ChildElement.SetAttribute('decryption','False') 
$xmlDoc.configuration.'system.web'.InsertBefore($ChildElement,$xmlDoc.configuration.'system.web'.authentication) 
$xmlDoc.Save($fileName) 
+2

你是否遇到問題?如果是這樣,那是什麼? – JLRishe

回答

0

我不確定什麼阻止您從多次呼叫SetAttribute()

$ChildElement.SetAttribute('decryption','False') 
$ChildElement.SetAttribute('validation','SHA1') 
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" /> 
+0

配置文件從該特定位置保存了一些問題。作品。謝謝。快速的問題我們如何檢查特定節點是否存在,並在不存在時添加。使用foreach? – Nishant

+0

'if($ xmldoc.'system.web'.SelectSingleNode(「machineKey」)-eq $ null){#element missing,add it}'。順便說一句。你確定你不想在授權之前添加它嗎? 'authentication'不是xml中的一個元素,所以它會被添加到'system.web'節點的末尾 –

+0

謝謝!是的,我將這一改變轉爲授權。 :) – Nishant