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

问题描述:

正如标题所说,我配置重写规则的反向代理功能有:在DSC脚本添加入站重写规则不起作用

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

为此,我使用PowerShell DSC,在脚本资源中设置配置。这对于出站规则来说非常合适,但入站规则不会被创建,也不会给出错误/警告。 当试图设置它的属性(后面的3行)时,警告会显示说它找不到入站规则,btw也显示正确使用的变量名称)。在IIS管理控制台中,它也不可见(是的,我已经使用了F5)。

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

  • 使用变量
  • 使用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资源,而不是脚本,并试图将1失败的命令放入Invoke-Command Scriptblock)后,我找到了解决方案。

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

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