Windows Vista Tips

Windows Vista Tips > Newsgroups > Windows Server > Scripting > querying wmi - recommended structure when iteration seems unnecessary?

Reply
Thread Tools Display Modes

querying wmi - recommended structure when iteration seems unnecessary?

 
 
James
Guest
Posts: n/a

 
      11-14-2008
Hello,

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 =
GetObject("winmgmts:{authenticationLevel=pktPrivac y}\\.\root\microsoftiisv2")

Set colItems = oWMI.ExecQuery("SELECT * FROM IIsFtpServer WHERE Name =
'MSFTPSVC/1'")

For Each oItem in colItems
oItem.Stop
Next
-------------

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:
colItems.Item(0).Stop
or
colItems(0).Stop

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


 
Reply With Quote
 
 
 
 
James
Guest
Posts: n/a

 
      11-14-2008
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

"Tom Lavedas" <> wrote in message
news:453b49a1-f84c-460f-8ffe-...
On Nov 14, 2:13 pm, "James" <no...@nowhere.com> wrote:
> Hello,
>
> 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 =
> GetObject("winmgmts:{authenticationLevel=pktPrivac y}\\.\root\microsoftiisv2")
>
> Set colItems = oWMI.ExecQuery("SELECT * FROM IIsFtpServer WHERE Name =
> 'MSFTPSVC/1'")
>
> For Each oItem in colItems
> oItem.Stop
> Next
> -------------
>
> 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:
> colItems.Item(0).Stop
> or
> colItems(0).Stop
>
> 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


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

colItems.Item(0).Stop

nor

colItems(0).Stop

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
***********
http://there.is.no.more/tglbatch/


 
Reply With Quote
 
urkec
Guest
Posts: n/a

 
      11-14-2008
"James" wrote:

> Hello,
>
> 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 =
> GetObject("winmgmts:{authenticationLevel=pktPrivac y}\\.\root\microsoftiisv2")
>
> Set colItems = oWMI.ExecQuery("SELECT * FROM IIsFtpServer WHERE Name =
> 'MSFTPSVC/1'")
>
> For Each oItem in colItems
> oItem.Stop
> Next
> -------------
>
> 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:
> colItems.Item(0).Stop
> or
> colItems(0).Stop
>
> 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
>
>
>



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:

http://msdn.microsoft.com/en-us/libr...50(VS.85).aspx


--
urkec
 
Reply With Quote
 
mayayana
Guest
Posts: n/a

 
      11-14-2008

> 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.


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".


 
Reply With Quote
 
James
Guest
Posts: n/a

 
      11-19-2008
cool, thanks.

"urkec" <> wrote in message
news:E71819F6-176C-44CA-B61F-...
> "James" wrote:
>
>> Hello,
>>
>> 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 =
>> GetObject("winmgmts:{authenticationLevel=pktPrivac y}\\.\root\microsoftiisv2")
>>
>> Set colItems = oWMI.ExecQuery("SELECT * FROM IIsFtpServer WHERE Name =
>> 'MSFTPSVC/1'")
>>
>> For Each oItem in colItems
>> oItem.Stop
>> Next
>> -------------
>>
>> 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:
>> colItems.Item(0).Stop
>> or
>> colItems(0).Stop
>>
>> 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
>>
>>
>>

>
>
> 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:
>
> http://msdn.microsoft.com/en-us/libr...50(VS.85).aspx
>
>
> --
> urkec



 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
xcacls.vbs stops in each iteration tom.doniphon Scripting 16 03-14-2008 11:21 AM
Unnecessary Ripping JRaj Windows Media Player 0 07-25-2007 12:24 PM
interface changes were UNnecessary... ahoier Internet Explorer 6 11-07-2006 06:35 AM
Unnecessary message Spirefm Windows Vista Mail 4 10-09-2006 01:20 PM
Alternative to "For Each" iteration Danny Scripting 3 05-31-2004 08:26 PM



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59