Dev*_*x93 5 iis powershell url-rewrite-module dsc
正如标题所示,我正在使用以下命令配置反向代理功能的重写规则:
为此,我使用 Powershell DSC,在脚本资源中设置配置。这对于出站规则非常有效,但不会创建入站规则,也不会给出错误/警告。当尝试在其上设置属性时(在后面的 3 行),确实会显示一条警告,指出它找不到入站规则,顺便说一句,还显示了正确使用的变量名称)。在 IIS 管理控制台中它也是不可见的(是的,我已经使用了 F5)。
我使用的是 IIS 本身生成的命令,它与我在网上看到的所有命令相同,包括 StackOverflow。这个特殊问题不容易找到,所以我一定错过了一些非常微不足道的东西。我已经尝试过:
我让命令工作的唯一方法是在(提升的)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"
}
}
Run Code Online (Sandbox Code Playgroud)
我希望有人知道为什么会发生这种情况,因为我在尝试解决这个问题时感到非常沮丧。
好吧,经过更多尝试(例如使用较新的 xScript 资源而不是 Script 并尝试将 1 个失败的命令放入 Invoke-Command Scriptblock 中),我找到了解决方案。
解决方案:在 Powershell 中,或者至少对于 IIS 配置,有两种方法将位置传递给命令。众所周知的是 -PSPath,但还有 -Location。当单独执行该命令(以及与其他命令一起)时,唯一起作用的是 passint -PSPath "IIS:\Sites\SITENAME"到该命令。我现在如何让它在我的脚本中为该命令工作是:-PSPath "IIS:\Sites" -Location "SITENAME"。
事实上,我需要使用此解决方法,在我看来,这是 Powershell 到 IIS 转换(或只是重写模块)中的一个错误。如果我错了,请纠正我!无论如何,我的问题已经解决,我希望这个答案可以帮助其他人将来解决这个问题。感谢那些查看我的问题并进行思考的人:)
| 归档时间: |
|
| 查看次数: |
1448 次 |
| 最近记录: |