2016-09-21 56 views
4

我想追加以下的web.config中的HttpHandler的部分:從字符串在PowerShell中創建XML節點,追加

<add name="Telerik_Web_UI_DialogHandler_aspx" verb="*" preCondition="integratedMode" path="Telerik.Web.UI.DialogHandler.aspx" type="Telerik.Web.UI.DialogHandler" /> 

目前我創建一個節點,設置這樣的屬性:

$xmlDoc = (Get-Content $webConfig) -as [Xml] 
$root = $xmlDoc.get_DocumentElement(); 

$handler1 = $xmlDoc.CreateNode('element',"add",'') 
$handler1.SetAttribute('name','Telerik_Web_UI_DialogHandler_aspx') 
$handler1.SetAttribute('verb','*') 
$handler1.SetAttribute('preCondition','integratedMode') 
$handler1.SetAttribute('path','Telerik.Web.UI.DialogHandler.aspx') 
$handler1.SetAttribute('type','Telerik.Web.UI.DialogHandler') 
$root.'system.webServer'.handlers.AppendChild($handler1); 

有沒有簡單的方法來將字符串轉換爲節點並追加它?或者只是在孩子的盡頭添加字符串?

+2

API命令不要修改'網絡更快,更好的可讀性。直接使用'appcmd.exe'或'WebAdministration'模塊(特別是['Add-WebConfigurationProperty'](https://technet.microsoft.com/en-us/library/ee790572.aspx)) –

回答

1

我找到了答案在這裏: https://stackoverflow.com/a/29693625/2165019

特別是如果你有很多的configs的改變,這是比使用在我看來

+1

I downvoted,因爲你的答案几乎完全是一個單一的鏈接。您應該修改鏈接答案中的示例代碼。 –