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

标签 powershell iis url-rewrite-module dsc

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

  • IIS 8.5
  • Windows 服务器 2012R2
  • URL 重写 2.1
  • 应用请求路由 3.0
  • Powershell 5.1

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

    我正在使用从 IIS 本身生成的命令,这与我在网上看到的所有内容相同,包括 StackOverflow。这个特殊的问题不容易找到,所以我一定遗漏了一些非常微不足道的东西。
    我已经尝试过:
  • 在不同的凭据下运行脚本
  • 使用硬编码名称而不是变量
  • 使用 URL 重写 2.0
  • 重新启动 IIS
  • 重启服务器

  • 我让命令工作的唯一方法是,如果它在(提升的)Powershell(下面用>>>>标记)中作为单个命令执行。

    然后是脚本本身。 (作为引用,还添加了一条出站规则):
    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"
                }
    }
    

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

    最佳答案

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

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

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

    关于powershell - 在 DSC 脚本中添加入站重写规则不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45284426/

    相关文章:

    c# - 我可以有一段 App_Code 与文件夹的其余部分分开编译吗?

    wcf - 添加新的 net.pipe 绑定(bind)时的绑定(bind)信息字段

    ssl - 重写 URL iis 7 - https ://www. somewhere.com -> https ://somewhere. com

    regex - 使用网址重写将www添加到网址

    asp.net - 重写sitemap.xml url - asp.net web.config

    performance - 为什么这个 PowerShell 代码 (Invoke-WebRequest/getElementsByTagName) 在我的机器上如此缓慢,但在其他机器上却没有?

    powershell - PowerShell命令输出的默认位置是什么?

    c# - 我应该如何运行后台服务来为 Windows 世界中的网站创建导出文件?

    PowerShell:从格式化为列表的输出中解析特定项目

    powershell - 在正在运行的 powershell 作业中写入外部数组