Sam H
Sam H

Reputation: 362

VBScript WMI PnPEntity obtaining HardwareID

I'm looking to extract the hardwareID from each device installed on a system using VBScript.

I can extract most properties from the PnPEntity class however the HardwareId or CompatibleId does seem to cause trouble - I'm presuming because it potentially returns an array.

My script is as follows:

Set TxtDriverOutput = objFSO.CreateTextFile("C:\Program Files\xxx\drivers.log", 8, True)
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")

txtDriverOutput.WriteLine Now() & Chr(32) & "Begin HardwareID WMI Query"
txtDriverOutput.WriteLine "----------------------------------------------------------------------------"
txtDriverOutput.WriteLine ""

Set colsHardwareID = objWmiService.ExecQuery("Select * from Win32_PnPEntity")
For Each objItem In colshardwareID
 For Each StrHardwareID In objItem.HardwareID
  txtdriveroutput.WriteLine StrHardwareID
 Next
Next

When I run the script, I see one hardware ID populated into the text file - and then I get Error: Object is not a collection flagged on the line of my second for loop (sometimes I have seen it flag the line after it errors for some reason so maybe take that with a pinch of salt.

I have tried using ObjItem.HarwareID.count, UBound(ObjItem.HardwareID) in case there is a PnP Device that doesn't have a hardware ID (not sure if possible). Can anyone point me in the right direction? Thanks!

Upvotes: 1

Views: 883

Answers (1)

Sam H
Sam H

Reputation: 362

I managed to get to the bottom of this in the end.

I had to use the following:

For Each objItem In colshardwareID
   If Not IsNull(objItem.HardwareID) Then 
      For Each StrHardwareID In objItem.HardwareID
        ReDim Preserve ArrHardwareID(i)
        ArrHardwareID(i) = strHardwareID
        i = i + 1
      Next
   End If
Next


As expected - the collection was empty. A .count or .isempty was not working, it had to be IsNull.

Upvotes: 1

Related Questions