【问题标题】:Get IIS HTTP Response Headers using Powershell (rawattributes)使用 Powershell (rawattributes) 获取 IIS HTTP 响应标头
【发布时间】:2020-07-30 14:38:38
【问题描述】:

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

具体来说:

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

我设法通过使用this solution 取得了一些进展,但我无法查询我想要的值的结果:

$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

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

【问题讨论】:

  • 如果您说您只需要这两个字符串,那么只需使用 Select-String cmdlet 或正则表达式匹配来选择它们以仅获取您想要的那些或删除您不想要的那些或选择数组位置。
  • 这很有意义,但是 Value 和 Name 不是成对出现的,而是作为连续的项目。

标签: powershell iis


【解决方案1】:

这里对如何输出此数据稍作改动。

$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
#>

您可以根据需要传入一个例外列表。

【讨论】:

  • 谢谢你,postanote。从这个例子中我不清楚,我如何获得不是“X-Powered-By”和“ASP.NET”的名称和值组合。可能有几十个,我需要全部获取。
  • 不用担心,但至于您的查询,这就是 Select-String 或 RegEx 匹配发挥作用的地方。
  • 谢谢!通过您的回答,我设法创建了一个完整的答案,并在此处发布。干杯!
  • 不用担心,很高兴知道它让您走上了所需的道路。
【解决方案2】:

感谢 postanote 的回答,我设法创建了一个完整的工作代码。

此代码在 IIS 默认网站的 HTTP 响应标头中检查不是特定名称和值组合的所有内容。所有异常都存储在一个数组中,以后可以检查和显示。

这适用于本地和继承的值。

$iisWebsiteName = "Default Web Site"
$IISManager = new-object Microsoft.Web.Administration.ServerManager 
$IISConfig = $IISManager.GetWebConfiguration($iisWebsiteName) #i.e. "Default Web Site" 
$httpProtocolSection = $IISConfig.GetSection("system.webServer/httpProtocol") 
$customHeadersCollection = $httpProtocolSection.GetCollection("customHeaders")
$customHeadersCollection = ($httpProtocolSection.GetCollection("customHeaders")) | Select-Object -Property RawAttributes
$customHeadersAtt = $customHeadersCollection.RawAttributes

$CustomHeadersList = @()

foreach ($CustomHeader in $customHeadersAtt) {
    if (($CustomHeader.name -ne "X-Powered-By") -and ($CustomHeader.value -ne "ASP.NET")) {
        $CustomHeadersList += ([pscustomobject]@{Name=$CustomHeader.name;Value=$CustomHeader.value})
    }
}

if ($CustomHeadersList) {
    Write-Output "There are custom HTTP Response Headers:" $CustomHeadersList
}
else {
    Write-Output "There are no relevant custom HTTP Response Headers"
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-12
    • 1970-01-01
    • 2020-01-17
    • 2016-09-21
    • 2017-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多