使用Powershell(属性)获取IIS HTTP响应头
在PowerShell中,我试图列出所有没有特定名称和值组合的HTTP响应头 具体而言: 名称不是X-Powered-By,值不是ASP.NET 我通过使用取得了一些进展,但我无法查询所需值的结果:使用Powershell(属性)获取IIS HTTP响应头,powershell,iis,Powershell,Iis,在PowerShell中,我试图列出所有没有特定名称和值组合的HTTP响应头 具体而言: 名称不是X-Powered-By,值不是ASP.NET 我通过使用取得了一些进展,但我无法查询所需值的结果: $iisWebsiteName = "Default Web Site" $IISManager = new-object Microsoft.Web.Administration.ServerManager $IISConfig = $IISManager.GetWebCon
$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
#>
您可以根据需要传入异常列表。多亏了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"
}
如果您说您只需要这两个字符串,则只需使用select String cmdlet或regex match选择它们,以仅获取您想要的字符串,或删除您不想要的字符串,或按数组位置选择。这很有意义,但是值和名称不会成对显示,而是作为连续项显示。谢谢,PostalNote。从这个示例中,我不清楚如何获得非X-Powered-By和ASP.NET的名称和值组合。可能有几十个,我需要把它们都弄到。不用担心,但是关于你在这里的查询,这就是Select字符串或RegEx匹配的作用。谢谢!有了你的答案,我在这里发布了一个完整的答案。干杯不用担心,很高兴知道它让你走上了需要的道路。