0

I do most of my scripting in VBScript and I often use WMI queries to get information from the system. The WMI ExecQuery method always returns a WMI collection object that I have to access from a For loop -- even in cases where you could only ever possibly return one item. Here is an example:

Set objWMI = GetObject("winmgmts:\\.\root\cimv2")
Set objColItems = objWMI.ExecQuery("SELECT * FROM Win32_BIOS",,48)

For Each objItem In objColItems
    strManufacturer = objItem.Manufacturer
Next

...

In this example, all I need is the brand of computer (i.e. Dell, HP, etc.). There should only ever be one instance of the Win32_BIOS class.

What's even more annoying, though, is when the class has multiple instances (say, Win32_Printers for example), and I know exactly which one I am looking for. Then I have to insert an If to check the name. It's an annoying amount of code when you're just looking for one piece of information from several classes.

I thought I saw a code sample once upon a time where someone accessed a property directly from an instance of a WMI class with a single line of code, but of course I didn't write it down.

So is there a way to do that without having to query for it and run it through a loop?


Update:

OK, I guess I just didn't Google hard enough, but I found out that what I am looking for is the SWbemServices.Get method. This allows you to get properties of a specific instance of a class without querying for it.

In the example they provide on that page, I can get the status of a service with just two lines of code:

Set objWMI = GetObject("winmgmts:\\.\root\cimv2").Get("Win32_Service.Name='Spooler'")
WScript.Echo objWMI.State

Neat. Except it only works in cases where there are multiple instances of the class and you know which one you're after. A good find, but how do I use it in the example I gave above, where there is only one instance?

Set objWMI = GetObject("winmgmts:\\.root\cimv2").Get("Win32_BIOS.Manufacturer")

Gives me an error: Invalid object path.

Set objWMI = GetObject("winmgmts:\\.root\cimv2").Get("Win32_BIOS")

That line works, but it's not bound to an instance so all I get are nulls.

How do I use this technique for single-instance classes? Like is there a way to specify the "default" instance?

Wes Sayeed
  • 177
  • 2
  • 12
  • `x = objColItems(0).Manufacturer`. Perhaps wmic easier, I use that. In a Command Prompt, `wmic bios get manufacturer` – Noodles Aug 21 '14 at 21:57

2 Answers2

0

On Vista and later, you can use the ItemIndex() property:

strManufacturer = objColItems.ItemIndex(0).Manufacturer
Bond
  • 16,071
  • 6
  • 30
  • 53
  • Close to what I asked for, but I kinda reworded the question a little to be more specific about what I want. Thanks though. – Wes Sayeed Aug 21 '14 at 23:45
-1
Set objWMI = GetObject("winmgmts:\\.root\cimv2").Get("Win32_BIOS=@")
WScript.Echo objWMI.Manufacturer