我正在尝试创建一个脚本,该脚本将查看我们客户的Web服务器,并确定他们是否将某些IIS重定向放入可能会引起问题的IIS重定向中。对于我们来说,这已经成为一个问题,而且虚拟目录的数量使这项工作变得非常繁琐。所以我想出了这个看起来很漂亮的脚本:
clear-host
#Import the WebAdministration Module
Import-Module WebAdministration
#Determine if Default Website is redirecting
write-host "Determining if redirects are enabled on the default website" -BackgroundColor DarkCyan
$root = "IIS:\Sites"
$all = get-childitem($root)
$all |
foreach{
$fold_name = $_.name
$cur_path = "$root" + "\" + "$fold_name"
$enable = (get-webconfigurationproperty -filter /system.webserver/httpRedirect -PSPath "$cur_path"-name enabled).value
If ($enable -eq $true)
{
$dest = (get-webconfigurationproperty -filter /system.webserver/httpRedirect -PSPath "$cur_path"-name destination).value
write-host $fold_name is redirected to $dest
}
else{
write-host "There is no redirect enabled on $fold_name"}
}
#Determine if redirect enabled on Virtual Directories
write-host "Determining if redirects are enabled on any virtual directories" -BackgroundColor DarkCyan
$Subroot = "IIS:\Sites\Default Web Site"
$Suball = get-childitem($Subroot)
$Suball |
foreach{
$Subfold_name = $_.name
$Subcur_path = "$Subroot" + "\" + "$Subfold_name"
$Subenable = (get-webconfigurationproperty -filter /system.webserver/httpRedirect -PSPath "$Subcur_path"-name enabled).value
If ($Subenable -eq $true)
{
$Subdest = (get-webconfigurationproperty -filter /system.webserver/httpRedirect -PSPath "$Subcur_path"-name destination).value
If ($Subdest -ne $dest)
{
write-host "$Subfold_name is redirected to $Subdest"}
}}
#End the script
write-host "Query complete. If any redirects exists they are listed above" -BackgroundColor DarkCyan
Pause
我在许多服务器上进行了尝试,并且运行良好……然后我上了一个并开始被红色淹没...
get-webconfigurationproperty : Filename: \\?\E:\Folder\Subfolder\web.config
Line number: 13
Error: There is a duplicate 'system.web.extensions/scripting/scriptResourceHandler' section defined
At E:\Scripts\IISRedirectCheck.ps1:49 char:18
+ $Subdest = (get-webconfigurationproperty -filter /system.webserver/httpRedi ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-WebConfigurationProperty], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException,Microsoft.IIs.PowerShell.Provider.GetConfigurationPropertyCommand
它指向第49行,即:
$Subdest = (get-webconfigurationproperty -filter /system.webserver/httpRedirect -PSPath "$Subcur_path"-name destination).value
当我测试$ Subdest时,它返回一个没有错误的值,所以我不确定这行有什么问题...有人知道吗?