Discussion Locked

Programming in WMI

0 helpful
4 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I am new in writing program in WMI. I have a syntax error is my first program in the For statement when run listed below.

Can anyone help me out?

dim objWMI

For Each objWMI In

GetObject("winmgmts:\root\cimv2").InstanceOf("Win32_Service")

Wscript.EchoobjWMI.Name

Next

4 replies

by zetacon4

Here would be my steps to resolving the error.

1. Instantiate your Win32_Service object before the for loop block.
2. Test the successful creation of the object before trying to use it.
3. Make sure it is a true collection with 1 or more children before using it.

! In any looping block, do not place dynamic value or object creation. Only one variable should change value in a for-next loop: the counter or childnode variable. Your objWMI variable should be that one varying element.

I hope this helps.

by juergen hartl

On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Service",,48)
For Each objItem in colItems
Wscript.Echo "Name: " & objItem.name
next

This discussion is closed to new replies.