Help formulating a more specific WMI query, not using a collection

When programming in WMI one typicall performs a WMI Query like "Select * from Win32_BIOS". The return is typically loaded into a collection and then we loop through the collection in a statement something along the lines of "For each objItem in wmiCollection".  The properties would then become (in the case of the BIOS serial number) objItem.SerialNumber.

I'd like to query specific information and assign it directly to a variable with out needlessly looping through a collection of properties I am going to discard anyway. Is there a way to skip the whole collection assignment / For/ Next loop ?

The code I am using looks like this (straight from the WMI Code Generation tool from M$)

             Try
                Dim connection As New ConnectionOptions
                connection.Username = userNameBox.Text
                connection.Password = passwordBox.Text
                connection.Authority = "ntlmdomain:DOMAIN"

                Dim scope As New ManagementScope( _
                    "\\FullComputerName\root\CIMV2", connection)
                scope.Connect()

                Dim query As New ObjectQuery( _
                    "SELECT * FROM Win32_BIOS")

                Dim searcher As New ManagementObjectSearcher(scope, query)

                For Each queryObj As ManagementObject In searcher.Get()

                    Console.WriteLine("-----------------------------------")
                    Console.WriteLine("Win32_BIOS instance")
                    Console.WriteLine("-----------------------------------")
                    Console.WriteLine("SerialNumber: {0}", queryObj("SerialNumber"))

                Next
                Close()
            Catch err As ManagementException
                MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
            Catch unauthorizedErr As System.UnauthorizedAccessException

                MessageBox.Show("Connection error (user name or password might be incorrect): " & unauthorizedErr.Message)
            End Try

 What I'd like to do is ditch the whole For Next loop and assign my end result directly to a variable;  Where the Query would be "SELECT SerialNumber from Win32BIOS"..... I'm creating a XML Driven routine and am only after exacting properties.

I'm VERY new to VB.NET. And even newer to trying to work with WMI through VB.NET.
LVL 1
jjthomas3Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
1) The ManagementObjectCollection does not have an indexer, so you can't reference by index.

2) The Get method only returns 1 item, so there isn't much hit to use a For statement.

3) I don't think that there is a way with WMI to get a single object.  Even the ManagementClass.GetInstances returns a ManagementObjectCollection.

Bob
0
jjthomas3Author Commented:
That's a bummer.

I know that when I perform "Select SerialNumber from Win32_BIOS" it only returns the serial number and no other members. It just feels like such a wast to me to loop through a collection to get one point of data.  I guess my thinking is more efficent than the language.

While it may not be very taxing to perform this simple loop is presents me with code that is needed only to compensate for Microsof't lack of vision on the small details.....If I can formulate a query that returns only one data-point I should be able to assign it straight to a variable.....

Lets see if anyone else has any suggestions for me..... although I have to say it is looking discouraging.

0
Bob LearnedCommented:
I agree that it is inefficient to loop to get a single value, but you need to look at the bigger picture to determine if it is even important to worry about inefficiency.  Most of WMI has more than 1 record, so I think that the developers didn't think about getting a single record.  Efficiency is really only important if you are looping 1000's of times--you won't notice any difference with a single record loop versus getting a single value.

Bob
0
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

grayeCommented:
Some WMI classes have a "Primary Key"... and will therefore allow you to ask for a specific instance without doing any looping.  For example, the DeviceID field in the Win32_LogicalDisk class is a primary key,  so you can get an instance of that one object with the following:

Dim obj as new System.Management.ManagementObject("\\" & RemotePC & "\root\cimv2:Win32_LogicalDisk.DeviceID='C:'");

Take a look at the following article (and downloadable source code).... there is specific mention of this "primary key" technique

http://home.hot.rr.com/graye/Articles/WMI_Intro.htm
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bob LearnedCommented:
WMI is such a big topic that I can always be learning new things.  *BIG GRIN*

Bob
0
jjthomas3Author Commented:
Graye,

 Thank you for the great link and the information. I'm trying to bring one of my freeware applications forward into the future because...well...I use it every day. The old version had hard-coded WMI Queries that looped through each various Win32_ classes, pulling out several pieces from each. I am trying to grapple with learning XML and get the new version to use a XML file so people can provide their own WMI queries.

LearnedOne, oh boy is it ever a big topic.....my head hurts :)
0
jjthomas3Author Commented:
Thank you, I managed to use the information and samples @ the site you provided to restructure my program a bit. I got much closer. There are just soooooooo many points of info out there on WMI.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.