我必须使用 Powershell 检查并设置 IIS 服务器中的“maxURL 请求过滤器”设置。我正在使用这些代码来检查这些设置的当前状态:
$(Get-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -filter "system.webServer/security/requestFiltering/requestLimits" -name "maxUrl").Value
据我了解,这些命令检查它在服务器中的设置方式,但也可以为每个站点设置它。服务器设置是否会强制关闭之前设置的所有应用程序和站点设置?如果没有,我如何检查所有网站和应用程序的这些内容?
在 IIS 中,可以在服务器级别、站点级别和应用程序级别设置配置。当在服务器级别更改配置时,通常会影响所有站点和应用程序,但在特定情况下,站点和应用程序级别配置可能会覆盖服务器级别配置。
如果要检查所有网站和应用程序的设置,则需要遍历每个网站和应用程序并检查其配置。
您可以使用以下 PowerShell 脚本:
# Get all sites on the server
$sites = Get-WebSite
foreach ($site in $sites) {
Write-Host "Site: $($site.Name)"
# Get the site's configuration
$siteConfig = $site.PSPath + '/system.webServer/security/requestFiltering/requestLimits'
# Check if the setting is overridden at the site level
if (Test-Path $siteConfig) {
$maxUrl = (Get-WebConfigurationProperty -PSPath $siteConfig -Name "maxUrl").Value
Write-Host "maxUrl setting at site level: $maxUrl"
} else {
# If not overridden, use the server level setting
$maxUrl = (Get-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST' -Filter "system.webServer/security/requestFiltering/requestLimits" -Name "maxUrl").Value
Write-Host "maxUrl setting at server level: $maxUrl"
}
# Get all applications under the site
$applications = Get-WebApplication -Site $site.Name
foreach ($app in $applications) {
Write-Host " Application: $($app.Path)"
# Get the application's configuration
$appConfig = $app.PSPath + '/system.webServer/security/requestFiltering/requestLimits'
# Check if the setting is overridden at the application level
if (Test-Path $appConfig) {
$maxUrl = (Get-WebConfigurationProperty -PSPath $appConfig -Name "maxUrl").Value
Write-Host " maxUrl setting at application level: $maxUrl"
} else {
# If not overridden, use the server level setting
Write-Host " maxUrl setting at server level: $maxUrl"
}
}
}