penguin359
penguin359

Reputation: 1489

How do I discover PCI information from an MSFT_NetAdapter

I am doing hardware inventory over all network adapters and need to record details including Speed, MAC address and PCI details. I've found most of what I need scanning over MSFT_NetAdapter and collecting those including the PCI Vendor and Device IDs, however, bus location information does not appear to be present.

After some googling, I discovered MSFT_NetAdapterHardwareInfoSettingData which had what I need, but I'm not sure how to programmatically go from one to the other. I have the PnPDeviceID from the first object which seems to include an instance number on the suffix, but I'm not sure how to use that to search in the other. As far as I can tell, only the Name field can be used to match it up, but that sounds like an unreliable solution. Is there a some way to make an exact search between the two classes of WMI objects?

I'm using Python 3.8.1 (64-bit) with the latest pywin32 and WMI 1.4.9 as a convenient wrapper for it, but I expect any solution described in terms of how to appropriately interact with WMI would be helpful.

Update: The answer from @JosefZ was what I was looking for. I took his example and made it a little more Pythonic in nature since I don't need the full flexibility of WQL at the moment:

import wmi

wnamespace = wmi.WMI(namespace="StandardCimv2")

for adapter in wnamespace.MSFT_NetAdapter():
    print('Adapter:', adapter.Name, adapter.InstanceID)
    for proprty in wnamespace.MSFT_NetAdapterHardwareInfoSettingData(InstanceID=adapter.InstanceID):
       print('Result:', proprty.InstanceID, adapter.Name, proprty.Name)

Upvotes: 0

Views: 795

Answers (1)

JosefZ
JosefZ

Reputation: 30123

The following code snippet could help. It pairs corresponding instances of MSFT_NetAdapter and MSFT_NetAdapterHardwareInfoSettingData wmi classes by their key properties (see provided links here as well as comments below):

import wmi

wnamespace = wmi.WMI( namespace="StandardCimv2")

# key property = DeviceID
wqlAdapter = "SELECT * FROM MSFT_NetAdapter"
wrxAdapter = wnamespace.query( wqlAdapter)

for adapter in wrxAdapter:
    # key property = InstanceID
    wql = "SELECT * FROM MSFT_NetAdapterHardwareInfoSettingData Where InstanceID='{}'".format(adapter.DeviceID)
    print( "querying adapter: {}".format(adapter.DeviceID)) # debugging 
    wrx = wnamespace.query( wql)
    for proprty in wrx:
        print( '  resulting data:', proprty.InstanceID, adapter.Name, proprty.Name )

print( '=== End Of Script ===')

Output from D:\bat\SO\59668995.py:

querying adapter: {E5531499-8F0E-4966-B3F8-C877A3BE8EF3}
  resulting data: {E5531499-8F0E-4966-B3F8-C877A3BE8EF3} wrdEthernet wrdEthernet
querying adapter: {7D8B1ECC-612C-4C3A-8999-0D91D24103BF}
=== End Of Script ===

Upvotes: 1

Related Questions