Page 1 of 1 1
Topic Options
#192187 - 2009-02-12 04:16 PM WMI process list returning no element array
coder Offline
Fresh Scripter

Registered: 2009-01-14
Posts: 39
I have s section of code that I dont know why the array is returning a empty array. This is the code I am running:

 Code:
Dim $strComputer, $objWMIService, $colProcessList, $objProcess
$strComputer = "."

$objWMIService = GetObject("winmgmts:\\" + $strComputer + "\root\cimv2")
$colProcessList = $objWMIService.ExecQuery("Select * from Win32_Process)")
If @error
  ? @Serror
  ?
EndIf
? UBound($colProcessList)


I am receiving no error, but am getting a -1 ubound. This is running on a XP Pro machine.

Any ideas?

Top
#192189 - 2009-02-12 04:27 PM Re: WMI process list returning no element array [Re: coder]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
$colProcessList is a collection of objects, not an array.
Top
#192191 - 2009-02-12 04:37 PM Re: WMI process list returning no element array [Re: Richard H.]
coder Offline
Fresh Scripter

Registered: 2009-01-14
Posts: 39
I added a For Each loop. It still will display nothing. Please forgive ignorance here. I have actually never tapped into the WMI system before.
 Code:
Dim $strComputer, $objWMIService, $colProcessList, $objProcess
$strComputer = "."

$objWMIService = GetObject("winmgmts:\\" + $strComputer + "\root\cimv2")
$colProcessList = $objWMIService.ExecQuery("Select * from Win32_Process)")
If @error
  ? @Serror
  ?
EndIf

For Each $objProcess in $colProcessList
  ? $objProcess.Name 
Next



My Ultimate goal here is to check for certain processes. If they do not exist, i want to launch the process.

Top
#192192 - 2009-02-12 04:47 PM Re: WMI process list returning no element array [Re: coder]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
You are not far off.

Be aware that there is a UDF (user defined function) in the UDF forum which will do all the work for you.

This is a test example which works on my XP pro system:
 Code:
Break ON
   
$=SetOption("Explicit","ON")
$=SetOption("WrapAtEOL","ON")
$=SetOption("ASCII","ON")
   
Global $oWMIService
Dim $oProcess,$colProcess
 
If Not IsDeclared($Host) Global $Host EndIf
 
If $Host="" $Host=@WKSTA EndIf
 
$oWMIService = GetObject("winmgmts:\\"+$Host+"\root\cimv2")
If @ERROR "Cannot access WMI sevrice, error is " @ERROR ": " @SERROR ?  Exit @ERROR EndIf
$colProcess=$oWMIService.ExecQuery('Select * from Win32_Process',,48)
If @ERROR "Cannot access WMI sevrice, error is " @ERROR ": " @SERROR ?  Exit @ERROR EndIf

For Each $oProcess In $colProcess
	"Process: "+$oProcess.Name+@CRLF
Next
 
Exit 0

Top
#192195 - 2009-02-12 05:14 PM Re: WMI process list returning no element array [Re: Richard H.]
coder Offline
Fresh Scripter

Registered: 2009-01-14
Posts: 39
What UDF would that be?

Thanks

Top
#192196 - 2009-02-12 05:21 PM Re: WMI process list returning no element array [Re: coder]
BradV Offline
Seasoned Scripter
****

Registered: 2006-08-16
Posts: 687
Loc: Maryland, USA
I'm not sure which specific one Richard was thinking about, but just go to the UDF forum. Click on the first link by NT Doc and then the UDF library connection link. In the window that comes up, look down the left column. There is WMIProcessList by Glenn, WMIQuery by Radimus, and WMIService. Look at those and see if at least one will do what you want.

Regards,

Brad

Top
#192204 - 2009-02-12 07:24 PM Re: WMI process list returning no element array [Re: BradV]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
WMIService is a powerful UDF if you are comfortable manipulating the WMI service commands. WMISvcMgr puts an easy to use wrapper around most of the common service functions (ListAll, Status, start/stop, modify, & remove). An old version is here on Korg and the latest on the resources page of my web site. Both of these are used for service control and management.

WMIQuery allows you to make simple queries. It's good for one-off queries or testing, but performance suffers if you need to make many queries, since it instantiates a new pointer each call.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 2220 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.07 seconds in which 0.038 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org