Adding Inbound Rewrite rule to DSC script doesn't work

As the title says, I set up rewrite rules for reverse proxy functions with:

  • IIS 8.5
  • Windows Server 2012R2
  • URL Rewrite 2.1
  • Application Request Routing 3.0
  • Powershell 5.1

For this, I use Powershell DSC by setting the configuration in the Script resource. This is fine for outbound rules, but no inbound rule is created and gives no error / warning. When trying to set properties on it (three lines after), the warning shows that it cannot find the incoming rule and also displays the correct variable name). Not visible in IIS Management Console either (and yes, I used F5 on it).

I am using the command as generated from IIS itself, which is equal to everything I see online including StackOverflow. This particular problem is not easy to find, so I must be missing something very trivial. I've already tried:

  • running Script under different credentials
  • using a hardcoded name instead of a variable
  • using URL Rewrite 2.0
  • restart IIS
  • server restart

The only way I got the command was to execute it as one command in (elevated) Powershell (marked below -> ->).

Then Script itself. (add one outbound rule for reference):

SetScript = {
            Write-Verbose "Checking if inbound rewrite rule is present..."

            $inbound = (Get-WebConfiguration -Filter "//System.webServer/rewrite/rules" -PSPath "IIS:\Sites\$using:frontendSiteName").Collection | Where-Object {$_.Name -eq "$using:inboundRuleName"}

            if($inbound -eq $null) {
                Write-Verbose "Inbound rewrite rule not present, configuring..."

        >>>>    Add-WebConfigurationProperty -Filter "//System.webServer/rewrite/rules" -Name "." -Value @{name=$using:inboundRuleName;stopProcessing="True"} -PSPath "IIS:\Sites\$using:frontendSiteName"
                Set-WebConfigurationProperty -Filter "//System.webServer/rewrite/rules/rule[@name='$using:inboundRuleName']/match" -Name "url" -Value "^api/(.*)" -PSPath "IIS:\Sites\$using:frontendSiteName"
                Set-WebConfigurationProperty -Filter "//System.webServer/rewrite/rules/rule[@name='$using:inboundRuleName']/action" -Name "type" -Value "Rewrite" -PSPath "IIS:\Sites\$using:frontendSiteName"
                Set-WebConfigurationProperty -Filter "//System.webServer/rewrite/rules/rule[@name='$using:inboundRuleName']/action" -Name "url" -Value "http://localhost:8000/api/{R:1}" -PSPath "IIS:\Sites\$using:frontendSiteName"

                Write-Verbose "Inbound rewrite rule configured"
            }

            Write-Verbose "Checking if outbound HTML rule is present..."

            $outboundHTML = (Get-WebConfiguration -Filter "//System.webServer/rewrite/outboundRules" -PSPath "IIS:\Sites\$using:frontendSiteName").Collection | Where-Object {$_.Name -eq "$using:outboundHTMLRuleName"}

            if($outboundHTML -eq $null) {
                Write-Verbose "Outbound HTML rule not present, configuring..."

                Add-WebConfigurationProperty -Filter "//System.webServer/rewrite/outboundRules/preConditions" -Name "." -Value @{name='IsHTML'} -PSPath "IIS:\Sites\$using:frontendSiteName"
                Add-WebConfigurationProperty -Filter "//System.webServer/rewrite/outboundRules/preConditions/preCondition[@name='IsHTML']" -Name "." -Value @{input='{RESPONSE_CONTENT_TYPE}';pattern='^text/html'} -PSPath "IIS:\Sites\$using:frontendSiteName"

                Add-WebConfigurationProperty -Filter "//System.webServer/rewrite/outboundRules" -Name "." -Value @{name=$using:outboundHTMLRuleName;preCondition='IsHTML'} -PSPath "IIS:\Sites\$using:frontendSiteName"
                Set-WebConfigurationProperty -Filter "//System.webServer/rewrite/outboundRules/rule[@name='$using:outboundHTMLRuleName']/match" -Name "filterByTags" -Value "A" -PSPath "IIS:\Sites\$using:frontendSiteName"
                Set-WebConfigurationProperty -Filter "//System.webServer/rewrite/outboundRules/rule[@name='$using:outboundHTMLRuleName']/match" -Name "pattern" -Value "^/(.*)" -PSPath "IIS:\Sites\$using:frontendSiteName"

                Add-WebConfigurationProperty -Filter "//System.webServer/rewrite/outboundRules/rule[@name='$using:outboundHTMLRuleName']/conditions" -Name "." -Value @{input='{URL}';pattern='^/api/.*'} -PSPath "IIS:\Sites\$using:frontendSiteName"

                Set-WebConfigurationProperty -Filter "//System.webServer/rewrite/outboundRules/rule[@name='$using:outboundHTMLRuleName']/action" -Name "type" -Value "Rewrite" -PSPath "IIS:\Sites\$using:frontendSiteName"
                Set-WebConfigurationProperty -Filter "//System.webServer/rewrite/outboundRules/rule[@name='$using:outboundHTMLRuleName']/action" -Name "value" -Value "/{C:1}/{R:1}" -PSPath "IIS:\Sites\$using:frontendSiteName"

                Write-Verbose "Outbound HTML rewrite rule configured"
            }
}

      

I hope someone knows why this is happening as I am very frustrated trying to solve this problem.

+3


source to share


1 answer


Ok, after trying a lot more (for example using a new xScript resource instead of Script and trying to put this 1 failing command in the Invoke-Command Scriptblock) I found a solution.

Solution: In Powershell, or at least for IIS config, there are two ways to pass the location to the command. The most famous is -PSPath, but there is also -Location. When running solo (and with other commands), what just works is the passint command -PSPath "IIS:\Sites\SITENAME"

. I got this job for this team in my Script, now this: -PSPath "IIS:\Sites" -Location "SITENAME"

.



The fact that I need to use this workaround seems to me to be a bug in the Powershell-to-IIS conversion (or just in the Rewrite module). If I am wrong, please correct me! Anyway, my problem is solved and I hope this answer helps someone else with this problem in the future. Thanks to the people who looked at my question and thought :)

0


source







All Articles