You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用PowerShell筛选非指定Name和Value的IIS HTTP响应头

Solution to Filter IIS Custom Headers

First, let's fix how we retrieve the headers so we have structured data to work with. Your current code extracts raw attributes as a flat list, which makes filtering tricky. Instead, we can convert each header into a PowerShell object with clear Name and Value properties:

$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")

# Convert each header to a structured object for easy filtering
$allHeaders = $customHeadersCollection | ForEach-Object {
    [PSCustomObject]@{
        Name  = $_.Attributes["name"].Value
        Value = $_.Attributes["value"].Value
    }
}

Now $allHeaders will show a clean, tabular list like this (example):

Name               Value
----               -----
X-Powered-By       ASP.NET
Referrer-Policy    no-referrer
X-Content-Type-Options nosniff

Filtering for Your Target Headers

You mentioned wanting headers that do not match the exact combination of Name = "X-Powered-By" AND Value = "ASP.NET". This means we keep all headers except that specific pair. Use this filter:

$filteredHeaders = $allHeaders | Where-Object {
    -not ($_.Name -eq "X-Powered-By" -and $_.Value -eq "ASP.NET")
}

If you actually meant a stricter filter—headers where Name is NOT "X-Powered-By" AND Value is NOT "ASP.NET" (excluding any header named X-Powered-By, regardless of value, and any header with value ASP.NET, regardless of name)—use this instead:

$filteredHeaders = $allHeaders | Where-Object {
    $_.Name -ne "X-Powered-By" -and $_.Value -ne "ASP.NET"
}

View the Results

Simply output the filtered list to see your target headers:

$filteredHeaders

Why This Works

  • Converting each configuration element to a PSCustomObject lets us use PowerShell's built-in Where-Object cmdlet for intuitive filtering.
  • We directly access the name and value attributes from the IIS configuration elements, avoiding the unstructured raw attribute output you were getting before.

内容的提问来源于stack exchange,提问作者bizna

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 16:22:38