在DSC脚本中添加入站重写规则不起作用

时间:2017-07-24 15:24:21

标签: powershell iis url-rewrite-module dsc

正如标题所说,我正在配置反向代理功能的重写规则:

  • IIS 8.5
  • Windows Server 2012R2
  • URL Rewrite 2.1
  • 应用程序请求路由3.0
  • Powershell 5.1

为此,我使用Powershell DSC,在脚本资源中设置配置。这对于出站规则非常有效,但是入站规则不会被创建,也不会给出错误/警告。 当试图在其上设置属性(在之后的3线),警告确实显示说这可'找不到入站规则,顺便说一句也显示了正确使用变量名)。在IIS管理控制台中,它也不可见(是的,我已在其上使用过F5)。

我使用从IIS本身生成的命令,这与我在网上看到的所有内容相同,包括StackOverflow。这个特殊的问题不容易找到,所以我必须遗漏一些非常微不足道的东西。 我已经尝试过了:

  • 以不同凭据运行脚本
  • 使用硬编码名称而不是变量
  • 使用URL Rewrite 2.0
  • 重新启动IIS
  • 重启服务器

我得到的命令工作的唯一途径是如果它'如在(升高)Powershell的单个命令(;>>>通过&GT以下标)S执行。

然后脚本本身。 (作为参考,也添加了一个出站规则):

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"
            }
}

我希望有人知道为什么会这样,因为我在试图解决这个问题时非常沮丧。

1 个答案:

答案 0 :(得分:0)

好的,在尝试了很多之后(比如使用较新的xScript资源而不是脚本并尝试将1个失败的命令放入Invoke-Command Scriptblock中)我找到了解决方案。

解决方案:在Powershell中,或者至少在IIS配置中,有两种方法可以将位置传递给命令。众所周知的是-PSPath,但也有-Location。 当执行命令solo(和其他命令)时,简单的工作是passint -PSPath "IIS:\Sites\SITENAME"到命令。 我现在如何在我的脚本中使用该命令:-PSPath "IIS:\Sites" -Location "SITENAME"

我需要使用此解决方法的事实在我看来,这是Powershell到IIS转换(或仅在Rewrite模块中)的错误。如果我错了,请纠正我! 无论如何,我的问题已经解决了,我希望这个答案能帮助其他人解决这个问题。 感谢那些关注我的问题的人并给了他一些思考:)