pythonwindowsnetwork-programmingwmiwindows-administration

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


Aim

To write a python script which fetches some hardware information of remote hosts (windows only) and I am using wmi library to connect to the remote host hardware information :

GPU Serial Number
Operating system version
GPU model Name
processor name

My environment

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

Problem

When I run the below code, it produces 100's of classes/functions, I don't even have an idea to use it to fulfill my need (get that hardware information)

Code

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
    ......
    ......
    ......

Final How to get that hardware information of remote hosts remotely using wmi library or any other possible way.


Solution

  • The wmi documentation is targeted for developers and IT administrators. You need to know where to find appropriate classes and their desired properties. The following commented script could help.

    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 )
    

    Please note that GPU Serial Number could be extracted from PNPDeviceID only if the hardware manufacturer implements it:

    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.

    Additional request: monitor details like serial Number, service tag, model name.

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