BrettJ
BrettJ

Reputation: 980

Add a <location /> element to applicationHost.config through PowerShell

Does anyone know of a way to add a new root level <location /> element to the applicationHost.config file via the WebAdministration cmdlets? There is Get-WebConfigurationLocation (and "Remove" and "Rename" versions of the cmdlet), but not an "Add." I've tried many different ways to do this using Add-WebConfiguration but I have not had any success.

Some of the things I've tried:

  Add-WebConfiguration -Filter '/' -AtIndex 0 -Value @{ location = @{ site='bobDev' } }
  Add-WebConfiguration -Filter '/' -AtIndex 0 -Value '<location site="bobDev" />'
  Add-WebConfiguration -Filter '/' -AtIndex 0 -Value @{value="bobDev.html" }

I know that the Values I'm trying aren't probably correct, but I'd at least like to see something get added into my applicationHost.config.

Upvotes: 4

Views: 2773

Answers (1)

mondnom
mondnom

Reputation: 381

Not sure if you solved your problem. It has been years. I came across your post. I just wanted to share how I solved my problem so that others can use it as needed.

This link helped a lot: Generate Script

The following lines will add a new location and also add parameters to your location.

Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -location 'mysite/devtest' -filter "system.webServer/security/ipSecurity" -name "." -value @{ipAddress='10.200.0.0';subnetMask='255.255.0.0';allowed='True'}

Set-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST' -location 'mysite/devtest' -filter "system.webServer/security/ipsecurity" -Name "."  -value @{allowUnlisted="false";enableProxyMode="true";denyAction="NotFound"}

Output:

  <location path="mysite/devtest">
    <system.webServer>
      <security>
        <ipSecurity allowUnlisted="false" enableProxyMode="true" denyAction="NotFound">
          <add ipAddress="10.200.0.0" subnetMask="255.255.0.0" allowed="true" />
        </ipSecurity>
      </security>
    </system.webServer>
  </location>

Upvotes: 4

Related Questions