querying wmi - recommended structure when iteration seems unnecessary?

Discussion in 'Scripting' started by James, Nov 14, 2008.

  1. James

    James Guest


    using wsh-vbscript-wmi

    somewhat new to wmi, working on a project now that has a lot of need to use
    it... I've been accomplishing a lot of what I need by starting from code
    examples, mostly from MS MSDN sites and the scripting center. One thing that
    has been bothering me is that no matter what, the code examples allways seem
    to use the For Each construct to iterate through a collection, even when
    there should only be one object returned? I realize the object returned is a
    collection so you need to follow rules for accessing the collection items,
    even if there is only one member, but I was wondering if there was some
    other method I should be using when I know I'm only going to get one object
    returned, something that returns the non-collection object directly?

    here is example of what I come across a lot:
    Set oWMI =

    Set colItems = oWMI.ExecQuery("SELECT * FROM IIsFtpServer WHERE Name =

    For Each oItem in colItems

    now a lot of the times there is no WHERE clause and multiple objects are
    expected back but as you can see in this case there is a WHERE clause which
    will always only return one item (one ftp server).

    I assume I could eliminate the For Each loop by simply doing this:

    but I'm wondering if in these cases I'm supposed to be using a different
    technique all together, instead of the .ExecQuery? Again, something that
    returns the object directly? and most importantly I'm looking to find out
    whats recommended, as in best practices. I don't want to get this whole
    project done and then later down the road when I've found time to reseach
    using wmi more find out I've done it in a poor fashion.

    any input would be appreciated
    James, Nov 14, 2008
    1. Advertisements

  2. James

    James Guest

    thanks Tom,
    ya, I just tried what I thought would be alternatives and as you already
    siad, it doesn't work. I find it laborious as well but the info obtained is
    great... as for the main reason of my post, I find the whole for each loop
    unclear when you know you are just going to get one thing. I try to write my
    code as clear and direct as possible and doing this loop is not clear and
    direct, IMHO

    thanks again

    It seems to me that, IIRC, the WMI collections do not expose an index
    facility. That is, this syntax does NOT work ...




    In addition, an empty collection will cause the FOR to throw an error.

    All in all, MS made a hash of the WMI query responses, IMHO. Further,
    I find it's whole architecture to be laborious, verbose and a pain in
    the "A" to use. But it sure provides a wealth of information and
    functionality, so I use it (sometimes).

    Tom Lavedas
    James, Nov 14, 2008
    1. Advertisements

  3. James

    urkec Guest

    You can use SWBemServices.Get (or event the winmgmts: moniker) to get a
    single instance, but it requires that you specify the values for all key
    properties of that instance:

    urkec, Nov 14, 2008
  4. James

    mayayana Guest

    As Tom said, th whole WMI system is very poorly
    designed. I've never used very much of WMI, since a
    good deal of it is just tedious wrappers around better
    functionality. WMI is good for some things. Especially
    hardware info. But the people at MS apparently thought
    it was clever to structure the whole thing like SQL, so
    even for something like the system motherboard you
    have no choice but to loop through a dummy "collection".
    mayayana, Nov 15, 2008
  5. James

    James Guest

    James, Nov 19, 2008
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.