12

I would like to append the following to the httpHandler section of a web.config:

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

Currently I am creating a node and setting the attributes like this:

$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);

Is there an easy way to just convert the string to a node and append it? Or to just add the string at the end of the children?

mJay
  • 713
  • 3
  • 11
  • 23
  • 2
    Don't modify `web.config` files directly, use `appcmd.exe` or the `WebAdministration` module (specifically [`Add-WebConfigurationProperty`](https://technet.microsoft.com/en-us/library/ee790572.aspx)) – Mathias R. Jessen Sep 21 '16 at 16:29

2 Answers2

9

I like the here-array approach in the linked answer above. It's also very amenable to variable substitution as well, which is handy.

$xmlDoc = (Get-Content $webConfig) -as [Xml]

[xml]$InsertNode = @"
<add name="Telerik_Web_UI_DialogHandler_aspx" verb="*" preCondition="integratedMode" path="Telerik.Web.UI.DialogHandler.aspx" type="Telerik.Web.UI.DialogHandler" />
"@

$xmlDoc.configuration.'system.webServer'.handlers.AppendChild($xmlDoc.ImportNode($InsertNode.Add, $true))
L1ttl3J1m
  • 385
  • 5
  • 8
1

I found the answer here: https://stackoverflow.com/a/29693625/2165019

Especially if you have a lot of configs to be changed this is faster and better readable than using API commands in my opinion

Community
  • 1
  • 1
mJay
  • 713
  • 3
  • 11
  • 23
  • 3
    I downvoted because your answer consists almost entirely of a single link. You should adapt the example code in the linked answer. – Kenny Evitt Jul 19 '17 at 16:14