Python 和 WMI Win_32 类

问题描述 投票:0回答:1

如何迭代 WMI 的系统摘要和组件。

我不想打印诸如硬件资源、组件、软件环境之类的东西。 我也不想能够显示多媒体、CD-ROM、声音设备、显示器等组件...

我想使用 WMI 和 WIN32_classes。

到目前为止,我有这段代码,但它没有迭代。

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: \n\n")
# for computer_system in computer_systems:
#    print(computer_system.Name)

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

print("Software Environment:\n\n")
print("Systeem Drivers: \n ")
for driver in drivers:
    print(driver.Name)

print("\nPrintJob: \n")
for printJob in printJobs:
    print(printJob.Name)

print("\nNetwerk connecties: \n")
for netwerkConnectie in netwerkConnecties:
    print(netwerkConnectie.Name)
    
print("\nRunning Tasks: \n")
for runningTask in runningTasks:
    print(runningTask.Name)

print("\nServices: \n")
for service in services:
    print(service.Name)

print("\nStartup Programs: \n")
for startupProgram in startupPrograms:
    print(startupProgram.Name)

有关 WIN32_(和/或 CIM_ 类)的信息。系统摘要和组件概述。

python automation system wmi infrastructure-as-code
1个回答
0
投票

您需要访问返回对象的属性/wmi 类的属性

在这种情况下,

conn.Win32_Computersystem()
返回一个包含对象(字典)的单个项目,该对象具有许多属性,这是访问所需属性的一种方式:

import wmi

conn = wmi.WMI()

def comp_info():
    cs = conn.Win32_Computersystem()[0]  # accessing first element
    return [
            cs.ChassisSKUNumber,
            cs.Domain,
            cs.Manufacturer,
            cs.Model,
            cs.Name,
            cs.OEMStringArray,
            cs.PartOfDomain,
            cs.PrimaryOwnerName,
            cs.SystemFamily,
            cs.SystemSKUNumber,
            cs.SystemType,
            cs.TotalPhysicalMemory,
            cs.UserName,
            cs.Workgroup
            ]

# looping and printing through returned list   
for i in (comp_info()):
    print(i)

同样,您可以从各种 wmi 类访问其他属性

© www.soinside.com 2019 - 2024. All rights reserved.