Editing web.config

Hi,

I’m new here, so apologies if this is an obvious answer. I have tried to find a cmdlet that would achieve this edit, but I’m not having much luck. I would like to add the requestFiltering section at SITE level [ie: web.config].

Only examples I’ve found online would only achieve this at server-level via an appcmd command. So I’ve resorted to trying to write a script to edit the web.config directly, to inject the section into the section.

Is there anything obvious I can use, or can someone please suggest some quick code please?

Thanks in advance,
Carl.

A web config file is just a XML file. There are many articles on opening an XML file and adding a node, editing a property or element utilizing Powershell. Without seeing an example, it’s going to be difficult to help you. This site will eat html\XML, so you will need to use github if you do want to post an example of your XML and desired result.

https://gist.github.com/anonymous/be5c0077d7d18bb88a19ec4abd6f9112

Hi Rob,

Appreciate the reply. I have a web.config that already has the system.web tags, but want to insert the rest of the structure inside those tags.

Carl.

Take a look at this and see if you can figure it out: https://blogs.msdn.microsoft.com/sonam_rastogi_blogs/2014/05/14/update-xml-file-using-powershell/

Hi,

I’ll advise against using any xml commands to change web.config. If you have a different xml file, then by all means.

For web.config files, best to use Get-WebConfiguration and Get-WebConfigurationProperty and the equivalent Set commands.

https://www.iis.net/learn/manage/powershell/powershell-snap-in-changing-simple-settings-in-configuration-sections

https://technet.microsoft.com/en-us/library/ee807834.aspx

https://technet.microsoft.com/en-us/library/ee790572.aspx

@Rob: Thanks for the links, I shall check those out - looks promising.

@Arie: I started off looking at these, but from what I can gather, they only allow you to set these settings at server-level in IIS and not at site-level as required in this instance.

AFAIK, all you need to do is supply the path.

The commands will work to what ever depth you give the parameters you supply, as long as it exists.

Reading through the documentation notes it appears that those commands check against a schema which would prevent you from setting incorrect web.config items. Thanks for the reference, another feather in the cap.