如何使用 WMI -( Windows Management Instrumentation) 从 python 脚本获取远程系统硬件信息

How can I get remote system hardware information using WMI -( Windows Management Instrumentation) from python script

瞄准

编写一个 python 脚本来获取远程主机的一些硬件信息(仅 windows),我正在使用 wmi 库连接到远程主机的硬件信息:

GPU Serial Number
Operating system version
GPU model Name
processor name

我的环境

language - python 3
connecting remote hosts using wmi library (works)
remote hosts operating system: windows 7 or windows 10

问题

当我 运行 下面的代码时,它会产生 100 个 classes/functions,我什至不知道如何使用它来满足我的需要(获取硬件信息)

代码

import wmi
conn = wmi.WMI() 
for classes in conn.classes :
    print(classes)
    ......
    ......
    ......
    Win32_VideoConfiguration
    Win32_LoggedOnUser
    CIM_CompatibleProduct
    Win32_PnPDevicePropertyReal64Array
    Win32_AccountSID
    MSFT_NetCircularDependencyDemand
    CIM_BootOSFromFS
    Msft_WmiProvider_GetObjectAsyncEvent_Post
    Win32_SystemSystemDriver
    CIM_InstIndication
    ......
    ......
    ......

决赛 如何使用 wmi 库或任何其他可能的方式远程获取远程主机的硬件信息。

wmi 文档面向开发人员和 IT 管理员。您需要 知道 在哪里可以找到合适的 类 及其所需的属性。以下 评论 脚本可能有所帮助。

import wmi
conn = wmi.WMI()         # or # wmi.WMI("some_other_machine")

# Operating system & OS version
for os in conn.Win32_OperatingSystem():
    print( 'OS : ' + os.Caption + ", version " + os.Version )

# Processor name
for pr in conn.Win32_Processor():
    print( 'CPU: ' + pr.Name )

# GPU model Name
# GPU Serial Number - partial solution
for vc in conn.Win32_VideoController():
    print( 'GPU: ' + vc.Name + "\r\n     " + vc.PNPDeviceID )

请注意GPU Serial Number could be extracted from PNPDeviceID只有在硬件制造商实现它的情况下:

Looking at the PNPDeviceID value, break it up by "\".

  • The first piece it the bus type. For me, it is PCI.
  • The second section describes the card. There's a vendor code, model number, etc.
  • The last section contains a number separated by ampersands. The serial number is the second number in that list, formatted in hex.

额外要求: 监控详细信息,如序列号、服务标签、型号名称。

import wmi
conn = wmi.WMI()

# convert uint16[] array to string
def cnvrt( tup ): 
    return ''.join( [chr( x ) if x else '' for x in tup] )

# this is 'universal' DesktopMonitor (no useful details for Generic PnP Monitor?)
for umn in conn.Win32_DesktopMonitor():
    print( 'UMn: Name             {}'.format( umn.Name ) )
    print( 'UMn: PNPDeviceID      {}'.format( umn.PNPDeviceID ) )

# this is 'specific' DesktopMonitor (all useful details?)
con2 =  wmi.WMI(namespace='root/WMI')
for mon in con2.WmiMonitorID():
    print( 'Mon: Active           {}'.format(        mon.Active ) )
    print( 'Mon: InstanceName     {}'.format(        mon.InstanceName ) )
    print( 'Mon: ManufacturerName {}'.format( cnvrt( mon.ManufacturerName ) ) )
    print( 'Mon: ProductCodeID    {}'.format( cnvrt( mon.ProductCodeID    ) ) )
    print( 'Mon: SerialNumberID   {}'.format( cnvrt( mon.SerialNumberID   ) ) )
    print( 'Mon: UserFriendlyName {}'.format( cnvrt( mon.UserFriendlyName ) ) )