WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

WMI
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Get Process List via WMI


Using ObjectCollectionOpen

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
;Gathering processes performance data via WMI
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
computername=""
username=""
password=""
Locator = ObjectOpen("WbemScripting.SWbemLocator")
Service = Locator.ConnectServer(computername,"root/cimv2",username,password)
Security = Service.Security_
Security.ImpersonationLevel = 3
If LastError( ) == 1261
	sComment="Error: WMI missing or not installed. %@CRLF% Utility will now exit."
	Message("WMI Test:", sComment)
	Exit
EndIf

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
;getting list of running processes that incudes Instance Index for each process
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
binbuf1 = binaryalloc(10000)
colItems = Service.ExecQuery("Select * from Win32_PerfRawData_PerfProc_Process")
hEnum=ObjectCollectionOpen(colItems)
list =""
x=1
While 1
		Obj = ObjectCollectionNext(hEnum)
		If Obj == 0 Then Break
		If Obj.Name == "_Total" Then Break
		procname=Obj.Name
		procid=Obj.IDProcess
		EOD=BinaryEodGet(binbuf1)
		BinaryPokeStr(binbuf1,EOD, procname)
		EOD2=BinaryEodGet(binbuf1) 
		processcount = BinaryStrCnt( binbuf1, 0, EOD2-1, procname)
		processindex=StrCat(procname,"#", processcount-1,"|",procid)
		list = StrCat(list , @tab, processindex)
		ObjectClose(Obj)
EndWhile
ObjectCollectionClose(hEnum)
BinaryFree(binbuf1)
list = StrTrim(list)

ObjectClose(Security)
ObjectClose(Service)
ObjectClose(Locator)
Message("Process list with instances",list)
exit


Using FOREACH

Note: This only works with 2004B or newer.
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
;Gathering processes performance data via WMI
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 

Computername=""
username=""
password=""
Locator = ObjectOpen("WbemScripting.SWbemLocator")
Service = Locator.ConnectServer(computername,"root/cimv2",username,password)
Security = Service.Security_
Security.ImpersonationLevel = 3
If LastError( ) == 1261
	sComment="Error: WMI missing or not installed. %@CRLF% Utility will now exit."
	Message("WMI Test:", sComment)
	Exit
EndIf

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
;getting list of running processes:
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
processlist=""
ProcIDList=""
Instance = Service.InstancesOf("win32_process")
x=1
 

ForEach obj in Instance
	If Obj.Name == "_Total" Then Break
	procname=Obj.Name
	procid=Obj.ProcessId
	
	If processlist==""
		processlist=StrCat(procname, @tab)
	else
		processlist=StrCat(processlist, procname, @tab)
	Endif
	If ProcIDList==""
		ProcIDList=StrCat(procid, @tab)
	else
		ProcIDList=StrCat(ProcIDList, procid, @tab)
	Endif
	x=x+1
Next
ObjCount=x
ObjectClose(Instance)
 
Drop(procid, procname, Obj, hEnum, Instance, x)

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
;Finding ObjInstance-Index for each process
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
n=0
j=1
h=""
While 1
	procname=ItemExtract(j, processlist, @tab)
	If procname == "" Then Break
	Position=ItemLocate(procname, h, @tab)
	If Position !=0
		j=j+1
		Continue
	EndIf
	
	While @True
		Found=ItemLocate(procname, processlist, @tab)
		If Found !=0
			procid=ItemExtract(Found,ProcIDList,@tab)
			If procname == "System Idle Process" Then procname="Idle"
			a=StrCat(FileRoot(procname), "#", n,"|",procid)
			n=n+1
			processlist=ItemReplace(a,Found,processlist,@tab)
			If h==""
				h=StrCat(a, @tab)
			Else
				h=StrCat(h,a, @tab)
			Endif
		Else
			Break
		Endif
	EndWhile
	n=0
	j=j+1
EndWhile
Drop(procid, procname,a, found,j,n)
Message("",StrReplace(processlist,@tab,@cr))
exit

Article ID:   W16752
File Created: 2005:02:18:12:22:14
Last Updated: 2005:02:18:12:22:14