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/