powershell - 使用 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
我不知道如何查询此输出并获取相关项目,或者我是否正在以正确的方式进行调查。
解决方案
这是对如何输出此数据的轻微改动。
$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
#>
您可以根据需要传入异常列表。
推荐阅读
- python - 在csv python中保存for循环的输出
- python - 如何使用类在 Python 中绘图?
- windows - Docker Desktop 2.2+ 主机卷挂载的性能非常慢
- linux - 使用 shell 脚本忽略 csv 文件中的第一行(标题)
- java - Android Studio - 错误代码:1(SQLITE_ERROR)原因:SQL(查询)错误缺少数据库
- scala - 带蜂巢的父子火花scala递归udf
- android - 保存用户添加的动态按钮android
- objective-c - Swift 中的 Objective-C NS_OPTIONS
- python - 使用 Opencv 打开高分辨率图像
- python - 语言参考中描述的python函数的描述符协议在哪里?