首页 > 解决方案 > 使用 Powershell (rawattributes) 获取 IIS HTTP 响应标头

问题描述

在 PowerShell 中,我试图列出所有没有名称和值的特定组合的 HTTP 响应标头。

具体来说:

名称不是“X-Powered-By”并且值不是“ASP.NET”

通过使用此解决方案,我设法取得了一些进展,但我无法查询我想要的值的结果:

$iisWebsiteName = "Default Web Site"
$IISManager = new-object Microsoft.Web.Administration.ServerManager 
$IISConfig = $IISManager.GetWebConfiguration($iisWebsiteName)
$httpProtocolSection = $IISConfig.GetSection("system.webServer/httpProtocol") 
$customHeadersCollection = $httpProtocolSection.GetCollection("customHeaders")
$customHeader = $customHeadersCollection | Select-Object rawattributes | Select-Object -Expand *

这就是我得到的回应:

X-Powered-By
Referrer-Policy
ASP.NET
no-referrer

我不知道如何查询此输出并获取相关项目,或者我是否正在以正确的方式进行调查。

标签: powershelliis

解决方案


这是对如何输出此数据的轻微改动。

$iisWebsiteName          = "Default Web Site"
$IISManager              = new-object Microsoft.Web.Administration.ServerManager 
$IISConfig               = $IISManager.GetWebConfiguration($iisWebsiteName)

$httpProtocolSection     = $IISConfig.GetSection("system.webServer/httpProtocol") 
$customHeadersCollection = ($httpProtocolSection.GetCollection("customHeaders")) | 
                            Select-Object -Property RawAttributes
$customHeadersCollection.RawAttributes
# Results
<#
Key   Value       
---   -----       
name  X-Powered-By
value ASP.NET 
#>

$customHeadersCollection.RawAttributes.name
# Results
<#
X-Powered-By
#>

$customHeadersCollection.RawAttributes.Values
# Results
<#
X-Powered-By
ASP.NET
#>

$customHeadersCollection.RawAttributes.Values[0]
# Results
<#
X-Powered-By
#>

$customHeadersCollection.RawAttributes.Values[1]
# Results
<#
ASP.NET
#>

更新

根据您在下面的评论。有多种过滤内容的方法。比较运算符是第一个开始的地方。

$customHeadersCollection.RawAttributes.Values -ne 'ASP.NET'
# Results
<#
X-Powered-By
#>

$customHeadersCollection.RawAttributes.Values -ne 'X-Powered-By'
# Results
<#
ASP.NET
#>

$customHeadersCollection.RawAttributes.Values -notmatch 'ASP'
# Results
<#
X-Powered-By
#>

您可以根据需要传入异常列表。


推荐阅读