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

PowerShell查询杀毒软件状态并过滤Windows Defender的问题求助

问题原因与修复方案

你的代码存在两处核心问题导致Windows Defender数据未被过滤:

  1. 过滤逻辑执行前就直接输出了所有杀毒软件的productState,没有排除Windows Defender的对应条目,因此会提前返回Defender的状态码
  2. 你将displayName和productState拆分存储为独立数组,破坏了每个杀毒软件「名称-状态」的绑定关系,后续仅过滤名称无法同步过滤对应状态

修复后完整代码
clear
function Get-AntivirusInfo { 
[cmdletBinding()]     
param ( 
[string]$ComputerName = "$env:computername" , 
$Credential 
)
    # 解码productState为可读状态,参考SecurityCenter2官方编码规则
    function Convert-AVState {
        param($stateCode)
        $hexStr = [Convert]::ToString($stateCode, 16).PadLeft(6, '0')
        $runState = $hexStr.Substring(2, 2)
        $sigState = $hexStr.Substring(4, 2)
        return [PSCustomObject]@{
            运行状态 = if ($runState -in '10','11') {'正常运行'} else {'未启动'}
            病毒库状态 = if ($sigState -eq '00') {'已更新'} else {'需升级'}
        }
    }

    $wmiQuery = "SELECT * FROM AntiVirusProduct" 
    $allAVProducts = Get-WmiObject -Namespace "root\SecurityCenter2" -Query $wmiQuery @psboundparameters

    # 先过滤掉所有Windows Defender相关条目
    $thirdPartyAV = $allAVProducts | Where-Object {$_.displayName -notmatch 'Windows Defender'}
    $hasOnlyDefender = ($allAVProducts.Count -eq 1 -and $allAVProducts.displayName -match 'Windows Defender')

    if ($allAVProducts.Count -eq 0) {
        Write-Output "未安装任何杀毒软件"
        return
    }

    if ($hasOnlyDefender) {
        Write-Output "设备仅安装了Windows Defender,无第三方杀毒软件"
        return
    }

    # 输出所有第三方杀软的名称与状态
    Write-Output "第三方杀毒软件列表:"
    foreach ($av in $thirdPartyAV) {
        $stateInfo = Convert-AVState $av.productState
        Write-Output "名称:$($av.displayName) | 运行状态:$($stateInfo.运行状态) | 病毒库状态:$($stateInfo.病毒库状态)"
    }
}
Get-AntivirusInfo

核心修改点
  • 优先对WMI返回的原始杀毒软件对象进行过滤,从根源排除Windows Defender的所有数据,不会出现遗落状态码的问题
  • 保留每个杀毒软件名称与状态的绑定关系,避免出现名称与状态不匹配的错误
  • 新增productState解码逻辑,直接输出用户可读的运行状态、病毒库状态,无需手动解析状态码

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 11:24:05