How can I pull out the values for rewriteMaps and GlobalRules in applicationHost.config with PowerShell?

1.7k Views Asked by At

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.

1

There are 1 best solutions below

0
On

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.

# Get the path and name of the redirect file, the
# ApplicationHost.Config file
Param([string]$redirectFile)

# Constants that will be necessary
[string]$redirListDir = $null
# Using a CSV to make it simpler to reorder
[string]$redirectList = "redirectList.csv"

# Check that we have a file to pull redirects from
if ( ($redirectFile -eq $null) -or ($redirectFile -eq "")) {
  "Hey, you need to give a file to check.  Make sure its the path including the name "
  "of the applicationHost.config that needs to be checked.  Try something like "
  ".\redirectReview.ps1 C:\FileLocation\applicationHost.config"
  "Thank you!"
  exit
}

# Check that we have a place to put the file at the end
    if(($redirListDir -eq $null) -or ($redirListDir -eq "")) {
        if (Test-Path -Path "d:\temp") {
            $redirListDir = "d:\temp"
      "Using D:\Temp"
        } elseif (Test-Path -Path "c:\temp") {
            $redirListDir = "c:\temp"
      "Using C:\Temp"
        } else {
            "Cannot find a Temp directory as D:\temp or C:\temp.  Create one and try again.`n"
            exit
        }
  }

# Clean up any existing file so we can create a new one
$redirectListFile = Join-Path -Path $redirListDir  -ChildPath($redirectList)
if (Test-Path $redirectListFile) {
  Remove-Item $redirectListFile -Force
}

# Open the file and put it in an XML format
[xml]$redirectXML = Get-Content $redirectFile

# Review the XML tags

# This gets all the rewrite Map redirects
$rules = $redirectXMl.configuration."system.webServer".rewrite.rewriteMaps.rewriteMap.add 

"Now for the Global Rules"
# This gets all the rules that have been added
$redirectUrl = $redirectXMl.configuration."system.webServer".rewrite.globalRules.rule

"Creating the output file"
# Output the claimed values into a CSV file that can be reviewed somewhere
$rules | % {
  $a = $_.key + "`t" + $_.value
  $a >> $redirectListFile
}
$redirectUrl | % {
  $b1 = ($_.match).OuterXML
  $b2 = ($_.action).OuterXML
  # Probably a better way to handle this but I need to match up the two properties
  $b = $b1 + "`t" + $b2
  $b >> $redirectListFile
}
# We should be done here