I am dealing with a large amount of redirects in our applicationHost.config file, basically I want to read the redirect rules and pull out the XML information for the rules so I can get the redirects URLs for review. Since we have a large set of redirect rules in our IIS 7.5 instance, put in using the URLRewrite Feature for IIS .
I want to export a listing of the rules we have in place, and since the applicationHost.config is XML using PowerShell 2.0 (I can't use 3.0 where I am yet) I should be able to just go through the nodes and pull them out. Simple I thought until I tried to get anything under the sectionGroup system.webServer and when I try to pull out that node I can't get any values to appear.
When looking at the config file a basic rule is structured (as per the links to Microsoft above):
<rule name="Administration Redirection" enabled="true" stopProcessing="true">
<match url="^administration[/]?.*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Redirect" url="http://www.mysite.fake/{R:0}" />
</rule>
Basically I have the intent to do something like the following:
[xml]$redirectXML = Get-Content $redirectFile
$redirectXML | % {$_.configuration.system.webServer.rewrite.rewriteMaps}
Where I want to get the rewriteMap key & value, so I have those redirects and can pull out the key and the redirected URL from the rules like this:
$redirectXML | % {$_.configuration.system.webServer.rewrite.globalRules.rule}
Here I want to link up the values in the and which will get me the majority of our redirect URLs.
It looked like the system.webServer is messing up the nodes somehow, since it has the . in the name. An option that seemed to be available was using configuration.configSections since that shows the sectionGroups, one of them is system.webServer, but I haven't figured out how to get the nodes in the right format to pull out the section I want.
Is there a way to do this, or a better way than trying to use the Get-Content? I've done other XML files in that before and while I have seen some other PowerShell script for applicationHost.config most are for adding, all I want to do is extract a couple of nodes.
Thanks for any help or pointers.
EDIT: So while still looking around I found out I can get past system.webServer by using " marks around it. Like so:
$redirectXMl.configuration."system.webServer".rewrite.rewriteMaps.rewriteMap.add
This gets me the Rewrite Maps in a nice sort of table of key, value pairs. Now I still need to get the GlobalRules. I can get each Rule with the following:
$redirectXMl.configuration."system.webServer".rewrite.globalRules.rule
which displays:
name : Campaign Redirect
enabled : true
stopProcessing : true
match : match
conditions : conditions
action : action
But then I need to pull out the values for match and action, not there yet.
After some work through the XMl, and finding some other answers that pointed me in the right direction I ended up with the following script. It could probably be cleaner but basically I was looking to pull out the portion of the applicationHost.config that handles the rewrites/redirects so I can manage the 100 or so we deal with on our site. It's hard to keep track of them on a different document so I wanted the ability to pull out what I wanted at any time.
This is in PowerShell v2.0, which is what we use for scripting. Comments mostly included to give an idea of what I am doing.