33
44<#
55. SYNOPSIS
6- Pulls out URL Rewrite Rules from the web.config and applicationHost.config file to return a Hashtable of those settings .
6+ Pulls out URL Rewrite Rules from the web.config and applicationHost.config file to return inbound and outbound rules .
77. DESCRIPTION
88 This is a function that is designed to pull out the URL Rewrite Rules that are set on a location of IIS.
9+ It extracts both inbound rules (from rewrite/rules) and outbound rules (from rewrite/outboundRules).
910 Because you can set it on an individual web.config file or the parent site(s), or the ApplicationHostConfig file for the location
1011 We need to check all locations to properly determine what is all set.
1112 The ApplicationHostConfig file must be able to be converted to Xml, but the web.config file doesn't.
1415 2. ApplicationHost.config file for the same location
1516 3. Then move up one level (Default Web Site/mapi -> Default Web Site) and repeat 1 and 2 till no more locations.
1617 a. If the 'clear' flag was set at any point, we stop at that location in the process.
18+ b. Inbound and outbound rules track the 'clear' flag independently.
1719 4. Then there is a global setting in the ApplicationHost.config file.
1820#>
1921function Get-URLRewriteRule {
2022 [CmdletBinding ()]
21- [OutputType ([hashtable ])]
23+ [OutputType ([PSCustomObject ])]
2224 param (
2325 [Parameter (Mandatory = $true )]
2426 [System.Xml.XmlNode ]$ApplicationHostConfig ,
@@ -31,51 +33,82 @@ function Get-URLRewriteRule {
3133 begin {
3234 Write-Verbose " Calling: $ ( $MyInvocation.MyCommand ) "
3335 $urlRewriteRules = @ {}
36+ $urlOutboundRewriteRules = @ {}
3437 $appHostConfigLocations = $ApplicationHostConfig.configuration.Location.path
3538 }
3639 process {
3740 foreach ($key in $WebConfigContent.Keys ) {
3841 Write-Verbose " Working on key: $key "
3942 $continue = $true
40- $clear = $false
43+ $clearInbound = $false
44+ $clearOutbound = $false
4145 $currentKey = $key
4246 $urlRewriteRules.Add ($key , (New-Object System.Collections.Generic.List[object ]))
47+ $urlOutboundRewriteRules.Add ($key , (New-Object System.Collections.Generic.List[object ]))
4348
4449 do {
4550 Write-Verbose " Working on currentKey: $currentKey "
4651 try {
4752 # the Web.config is looked at first
4853 [xml ]$content = $WebConfigContent [$currentKey ]
49- $rules = $content.configuration .' system.webServer' .rewrite.rules
5054
51- if ($null -ne $rules ) {
52- $clear = $null -ne $rules.clear
53- $urlRewriteRules [$key ].Add($rules )
54- } else {
55- Write-Verbose " No rewrite rules in the config file"
55+ if (-not $clearInbound ) {
56+ $rules = $content.configuration .' system.webServer' .rewrite.rules
57+
58+ if ($null -ne $rules ) {
59+ $clearInbound = $null -ne $rules.clear
60+ $urlRewriteRules [$key ].Add($rules )
61+ } else {
62+ Write-Verbose " No inbound rewrite rules in the config file"
63+ }
64+ }
65+
66+ if (-not $clearOutbound ) {
67+ $outboundRules = $content.configuration .' system.webServer' .rewrite.outboundRules
68+
69+ if ($null -ne $outboundRules ) {
70+ $clearOutbound = $null -ne $outboundRules.clear
71+ $urlOutboundRewriteRules [$key ].Add($outboundRules )
72+ } else {
73+ Write-Verbose " No outbound rewrite rules in the config file"
74+ }
5675 }
5776 } catch {
5877 Write-Verbose " Failed to convert to xml"
5978 Invoke-CatchActions
6079 }
6180
62- if (-not $clear ) {
81+ if (-not $clearInbound -or -not $clearOutbound ) {
6382 # Now need to look at the applicationHost.config file to determine what is set at that location.
6483 # need to do this because of the case sensitive query to get the xmlNode
65- Write-Verbose " clear not set on config. Looking at the applicationHost.config file"
84+ Write-Verbose " Looking at the applicationHost.config file"
6685 $appKey = $appHostConfigLocations | Where-Object { $_ -eq $currentKey }
6786
6887 if ($appKey.Count -eq 1 ) {
6988 $location = $ApplicationHostConfig.SelectNodes (" /configuration/location[@path = '$appKey ']" )
7089
7190 if ($null -ne $location ) {
72- $rules = $location .' system.webServer' .rewrite.rules
7391
74- if ($null -ne $rules ) {
75- $clear = $null -ne $rules.clear
76- $urlRewriteRules [$key ].Add($rules )
77- } else {
78- Write-Verbose ' No rewrite rules in the applicationHost.config file'
92+ if (-not $clearInbound ) {
93+ $rules = $location .' system.webServer' .rewrite.rules
94+
95+ if ($null -ne $rules ) {
96+ $clearInbound = $null -ne $rules.clear
97+ $urlRewriteRules [$key ].Add($rules )
98+ } else {
99+ Write-Verbose " No inbound rewrite rules in the applicationHost.config file"
100+ }
101+ }
102+
103+ if (-not $clearOutbound ) {
104+ $outboundRules = $location .' system.webServer' .rewrite.outboundRules
105+
106+ if ($null -ne $outboundRules ) {
107+ $clearOutbound = $null -ne $outboundRules.clear
108+ $urlOutboundRewriteRules [$key ].Add($outboundRules )
109+ } else {
110+ Write-Verbose " No outbound rewrite rules in the applicationHost.config file"
111+ }
79112 }
80113 } else {
81114 Write-Verbose " We didn't find the location for '$appKey ' in the applicationHostConfig. This shouldn't occur."
@@ -85,21 +118,34 @@ function Get-URLRewriteRule {
85118 }
86119 }
87120
88- if ($clear ) {
89- Write-Verbose " Clear was set, don't need to know what else was set."
121+ if ($clearInbound -and $clearOutbound ) {
122+ Write-Verbose " Clear was set for both inbound and outbound , don't need to know what else was set."
90123 $continue = $false
91124 } else {
92125 $index = $currentKey.LastIndexOf (" /" )
93126
94127 if ($index -eq -1 ) {
95128 $continue = $false
96- # look at the global configuration of the applicationHost.config file
97- $rules = $ApplicationHostConfig.configuration .' system.webServer' .rewrite.rules
98129
99- if ($null -ne $rules ) {
100- $urlRewriteRules [$key ].Add($rules )
101- } else {
102- Write-Verbose " No global configuration for rewrite rules."
130+ if (-not $clearInbound ) {
131+ # look at the global configuration of the applicationHost.config file
132+ $rules = $ApplicationHostConfig.configuration .' system.webServer' .rewrite.rules
133+
134+ if ($null -ne $rules ) {
135+ $urlRewriteRules [$key ].Add($rules )
136+ } else {
137+ Write-Verbose " No global configuration for inbound rewrite rules."
138+ }
139+ }
140+
141+ if (-not $clearOutbound ) {
142+ $outboundRules = $ApplicationHostConfig.configuration .' system.webServer' .rewrite.outboundRules
143+
144+ if ($null -ne $outboundRules ) {
145+ $urlOutboundRewriteRules [$key ].Add($outboundRules )
146+ } else {
147+ Write-Verbose " No global configuration for outbound rewrite rules."
148+ }
103149 }
104150 } else {
105151 $currentKey = $currentKey.Substring (0 , $index )
@@ -111,6 +157,9 @@ function Get-URLRewriteRule {
111157 }
112158 }
113159 end {
114- return $urlRewriteRules
160+ return [PSCustomObject ]@ {
161+ Inbound = $urlRewriteRules
162+ Outbound = $urlOutboundRewriteRules
163+ }
115164 }
116165}
0 commit comments