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

如何通过WMI及Win32类遍历系统摘要与组件?

问题解决:通过WMI遍历系统摘要、硬件组件与软件环境

问题描述

需要通过WMI和Win32类遍历系统摘要、硬件资源、指定组件(含多媒体、CD-ROM、音频设备、显示设备等)及软件环境信息,但现有Python代码无法实现完整遍历功能,同时需了解相关Win32/CIM类的用途。

原代码如下:

import wmi
c = wmi.WMI()

# List with Win32 components:

# https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-provider

computer_systems = c.Win32_Computersystem()
componenten = c.WIN32_SystemDevices()
drivers = c.Win32_SystemDriver()
printJobs = c.Win32_PrintJob()
netwerkConnecties = c.Win32_NetworkAdapter()
runningTasks = c.Win32_Process()
services = c.Win32_Service()
startupPrograms = c.Win32_StartupCommand()


# print("System Summary: 

")
# for computer_system in computer_systems:
#    print(computer_system.Name)

# print("Components: 
")
# for component in componenten:
#    print(component.Name)

print("Software Environment:

")
print("Systeem Drivers: 
 ")
for driver in drivers:
    print(driver.Name)

print("
PrintJob: 
")
for printJob in printJobs:
    print(printJob.Name)

print("
Netwerk connecties: 
")
for netwerkConnectie in netwerkConnecties:
    print(netwerkConnectie.Name)
    
print("
Running Tasks: 
")
for runningTask in runningTasks:
    print(runningTask.Name)

print("
Services: 
")
for service in services:
    print(service.Name)

print("
Startup Programs: 
")
for startupProgram in startupPrograms:
    print(startupProgram.Name)

代码修正方案

原代码存在两个核心问题:一是WIN32_SystemDevices类名大小写错误(WMI类名需严格使用Win32_SystemDevices);二是未启用系统摘要遍历,且未添加指定的多媒体、CD-ROM等组件对应的WMI类查询。以下是修正后的完整代码:

import wmi

# 初始化WMI连接
c = wmi.WMI()

# -------------------------- 系统摘要 --------------------------
print("=== 系统摘要 ===")
for system in c.Win32_ComputerSystem():
    print(f"计算机名称: {system.Name}")
    print(f"制造商: {system.Manufacturer}")
    print(f"型号: {system.Model}")
    print(f"总物理内存: {round(int(system.TotalPhysicalMemory)/1024/1024/1024, 2)} GB")
    print(f"登录用户名: {system.UserName}\n")

# -------------------------- 硬件组件 --------------------------
print("=== 硬件组件 ===")

# 系统设备
print("--- 系统设备 ---")
for device in c.Win32_SystemDevices():
    if device.Name:
        print(f"设备名称: {device.Name}")

# CD-ROM设备
print("\n--- CD-ROM设备 ---")
for cdrom in c.Win32_CDROMDrive():
    print(f"驱动器盘符: {cdrom.Drive}")
    print(f"设备型号: {cdrom.Caption}")

# 音频设备
print("\n--- 音频设备 ---")
for audio in c.Win32_SoundDevice():
    print(f"设备名称: {audio.Name}")
    print(f"制造商: {audio.Manufacturer}")

# 显示设备
print("\n--- 显示设备 ---")
for display in c.Win32_VideoController():
    print(f"显卡型号: {display.Name}")
    print(f"显存容量: {round(int(display.AdapterRAM)/1024/1024, 2)} MB")
    print(f"当前分辨率: {display.CurrentHorizontalResolution}x{display.CurrentVerticalResolution}\n")

# 多媒体设备(含摄像头等)
print("--- 多媒体设备 ---")
for media in c.Win32_MultimediaDevice():
    if media.Name:
        print(f"设备名称: {media.Name}")

# -------------------------- 软件环境 --------------------------
print("\n=== 软件环境 ===")

# 系统驱动
print("--- 系统驱动 ---")
for driver in c.Win32_SystemDriver():
    if driver.Name:
        print(f"驱动名称: {driver.Name}")
        print(f"运行状态: {driver.State}")

# 打印任务
print("\n--- 打印任务 ---")
jobs = c.Win32_PrintJob()
if jobs:
    for job in jobs:
        print(f"任务名称: {job.Name}")
        print(f"文档名称: {job.Document}")
else:
    print("当前无待处理打印任务")

# 网络适配器
print("\n--- 网络适配器 ---")
for adapter in c.Win32_NetworkAdapter():
    if adapter.Name:
        print(f"适配器名称: {adapter.Name}")
        print(f"MAC地址: {adapter.MACAddress}")

# 运行中进程
print("\n--- 运行中进程 ---")
for process in c.Win32_Process():
    if process.Name:
        print(f"进程名: {process.Name} (PID: {process.ProcessId})")

# 系统服务
print("\n--- 系统服务 ---")
for service in c.Win32_Service():
    print(f"服务名: {service.Name}")
    print(f"显示名称: {service.DisplayName}")
    print(f"服务状态: {service.State}\n")

# 开机启动程序
print("--- 开机启动程序 ---")
for startup in c.Win32_StartupCommand():
    print(f"程序名称: {startup.Name}")
    print(f"启动路径: {startup.Command}")

关键Win32/CIM类说明

系统摘要类

  • Win32_ComputerSystem: 获取计算机核心信息(名称、制造商、内存总量、登录用户等)
  • Win32_OperatingSystem: 补充操作系统版本、安装日期、系统目录等信息(可按需添加)

硬件组件类

  • Win32_SystemDevices: 遍历所有系统硬件设备
  • Win32_CDROMDrive: 读取CD/DVD驱动器的盘符、型号等信息
  • Win32_SoundDevice: 获取音频输入输出设备的名称、制造商信息
  • Win32_VideoController: 查询显卡型号、显存容量、当前显示分辨率
  • Win32_MultimediaDevice: 覆盖摄像头、通用多媒体接口等设备

软件环境类

  • Win32_SystemDriver: 系统驱动程序的名称、运行状态
  • Win32_PrintJob: 当前打印队列中的任务详情
  • Win32_NetworkAdapter: 网络适配器的名称、MAC地址等配置
  • Win32_Process: 正在运行的进程名称、PID
  • Win32_Service: 系统服务的名称、显示名、运行状态
  • Win32_StartupCommand: 开机自动启动的程序名称与启动路径

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 05:03:20