Werbung: SecurityConsole.de verwaltet Ihre Computer mit Security Essentails aus der Cloud!
30 Tage kostenfrei testen und 20% Rabatt für Ihre Bestellung mit Promocode: WBF2685582
(Promocode gültig bis 31.12.2011)

Group:  English: Windows Server » microsoft.public.windows.server.scripting
Thread: querying wmi - recommended structure when iteration seems unnecessary?

HTVi
TV Discussion Newsgroups

querying wmi - recommended structure when iteration seems unnecessary?
"James" <noone[ at ]nowhere.com> 11/14/2008 7:13:44 PM
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=pktPrivacy}\\.\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


Re: querying wmi - recommended structure when iteration seems unnecessary?
Tom Lavedas <tglbatch[ at ]cox.net> 11/14/2008 8:54:58 PM
On Nov 14, 2:13 pm, "James" <no...[ at ]nowhere.com> wrote:
[Quoted Text]
> 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=pktPrivacy}\\.\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/
Re: querying wmi - recommended structure when iteration seems unnecessary?
"James" <noone[ at ]nowhere.com> 11/14/2008 9:11:15 PM
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" <tglbatch[ at ]cox.net> wrote in message
news:453b49a1-f84c-460f-8ffe-76721606d0cf[ at ]i18g2000prf.googlegroups.com...
On Nov 14, 2:13 pm, "James" <no...[ at ]nowhere.com> wrote:
[Quoted Text]
> 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=pktPrivacy}\\.\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/


RE: querying wmi - recommended structure when iteration seems unnecess
urkec 11/14/2008 10:24:01 PM
"James" wrote:

[Quoted Text]
> 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=pktPrivacy}\\.\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/library/aa390350(VS.85).aspx


--
urkec
Re: querying wmi - recommended structure when iteration seems unnecessary?
"mayayana" <mayaXXyana[ at ]rcXXn.com> 11/15/2008 12:16:48 AM

[Quoted Text]
> 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".


Re: querying wmi - recommended structure when iteration seems unnecess
"James" <noone[ at ]nowhere.com> 11/19/2008 6:50:26 PM
cool, thanks.

"urkec" <urkec[ at ]discussions.microsoft.com> wrote in message
news:E71819F6-176C-44CA-B61F-0CE6375B5507[ at ]microsoft.com...
[Quoted Text]
> "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=pktPrivacy}\\.\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/library/aa390350(VS.85).aspx
>
>
> --
> urkec


Home | Search | Terms | Imprint Contact
Newsgroups Reader - provided by WiredBox.Net
Suche nach Orten, Städten, Postleitzahlen, Vorwahlen, Kfz-Kennzeichen