Editing web.config

This topic contains 7 replies, has 3 voices, and was last updated by Profile photo of Rob Simmers Rob Simmers 3 weeks, 4 days ago.

  • Author
    Posts
  • #57212
    Profile photo of Carl
    Carl
    Participant

    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.

  • #57266
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    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.

  • #57307
    Profile photo of Carl
    Carl
    Participant

    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.

  • #57400
    Profile photo of Rob Simmers
    Rob Simmers
    Participant
  • #57407
    Profile photo of Arie H
    Arie H
    Participant

    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

  • #57410
    Profile photo of Carl
    Carl
    Participant

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

    • #57413
      Profile photo of Arie H
      Arie H
      Participant

      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.

  • #57424
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    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.

You must be logged in to reply to this topic.