使用Powershell获取IIS HTTP响应标头(原始属性) [英] Get IIS HTTP Response Headers using Powershell (rawattributes)
问题描述
在PowerShell中,我试图列出所有没有名称和值的特定组合的HTTP响应标头.
In PowerShell, I am trying to list all the HTTP Response Headers that do not have a specific combination of Name and Value.
具体来说:
名称不是"X-Powered-By".AND值不是"ASP.NET"
Name is not "X-Powered-By" AND Value is not "ASP.NET"
通过使用此解决方案,我设法取得了一些进展,但我无法查询所需值的结果:
I managed to get some progress by using this solution, but I cannot manage to inquire into the results for the values I want:
$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
我不知道如何查询此输出并获取相关项目,或者我什至没有正确地研究它.
I have no idea how to query into this output and get the relevant items, or if I am even looking into it the right way.
推荐答案
此处对输出数据的方式略有改动.
Here is a slight alteration to how to output this data.
$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
#>
更新
根据您在下面的评论.有多种方法可以过滤内容.比较运算符是第一个开始的地方.
As per your comment below. There are a number of ways to filter content. Comparison operators are the first place to start.
$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
#>
您可以根据需要传入例外列表.
You can pass in a list of exceptions as needed.
这篇关于使用Powershell获取IIS HTTP响应标头(原始属性)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!