Page 1 of 1 1
Topic Options
#192750 - 2009-03-09 07:38 PM check if program is running
jeff_eisenberg Offline
Fresh Scripter

Registered: 2009-02-26
Posts: 45
Loc: CA
Is there a way kixtart can query if a program is running. Like see if xyz.exe is running in task manager??

thanks,

Jeff.

Top
#192753 - 2009-03-09 08:47 PM Re: check if program is running [Re: jeff_eisenberg]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
sure, many.

you could start with searching the EnumProcess() udf.
_________________________
!

download KiXnet

Top
#192754 - 2009-03-09 09:19 PM Re: check if program is running [Re: Lonkero]
jeff_eisenberg Offline
Fresh Scripter

Registered: 2009-02-26
Posts: 45
Loc: CA
So if I try to query for the PID of a process. Say:

$ie = EnumProcess('iexplore.exe')
MESSAGEBOX ($ie,"Test")

The value returned is "EnumProcess".

Is this syntax incorrect?

Top
#192755 - 2009-03-09 09:21 PM Re: check if program is running [Re: jeff_eisenberg]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
You neglected to load the UDF. You need to find the EnumProcess UDF, download it, and either paste it into your script or save it in a separate file and dynamically load it with
Call 'somepath\enumprocess.udf'

Then things should work better.

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

Top
#192756 - 2009-03-09 09:57 PM Re: check if program is running [Re: Glenn Barnas]
jeff_eisenberg Offline
Fresh Scripter

Registered: 2009-02-26
Posts: 45
Loc: CA
I can't seem to find it anywhere. There is an enumprocess.exe but I can't find any enumprocess.udf script file. Any suggestions on where to find?

thanks,

Jeff

Top
#192757 - 2009-03-09 10:25 PM Re: check if program is running [Re: jeff_eisenberg]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
I'd say "grab them from my web site where they're indexed", but the web server seems to be down. \:\( Will be another hour before I can get there to see what's going on. In the mean time, here are two UDFs I have in my library:

;; 
;;====================================================================== 
;; 
;;FUNCTION       WMIProcessList() 
;; 
;;ACTION         Return a list of process info from a computer 
;; 
;;AUTHOR         Glenn Barnas 
;; 
;;VERSION        1.1 / 2009/02/20 
;;		 Added CFlag support to return command line instead of Process Name 
;;		 1.0 / 2007/10/12 
;;		 Written as a replacement for PSList(), which uses SysInternals PSList.exe 
;; 
;;SYNTAX         WMIProcessList([Process] [, Computer] [, AuthPtr] [, CFlag]) 
;; 
;;PARAMETERS     Process - return information about a specific process (group) by name 
;;		   or individual process ID (PID) 
;; 
;;		 Computer - OPTIONAL, Name of computer to query 
;; 
;;               AuthPtr - OPTIONAL - pre-authenticated WMI object pointer 
;;                 Use WMIAuthentication() udf to create the AuthPtr value 
;;                 AuthPtr is not needed if user has admin rights 
;; 
;;		 CFlag - OPTIONAL - Returns the complete command line instead of the process name 
;; 
;;REMARKS        By default, returns a detailed list of all processes from the local computer. An 
;;		   alternate computer can be specified, as can a specific process. When specifying 
;;		   processes, you can use a name (cmd.exe) or process ID. If you specify a name, all 
;;		   processeses matching that name will be returned, while specifying a PID will return 
;;		   exactly one element that matches that process (if it was found) 
;;		   If no match is found, the function exits with status 2 (file not found) 
;; 
;;RETURNS        Array of comma-delimited values, one element per process: 
;;			Process Name 
;;			Process ID (PID) 
;;			Thread Count 
;;			Handle Count 
;;			Memory Usage (Bytes) 
;;			User Time	D:HH:MM:SS.sss format 
;;			Kernel Time	D:HH:MM:SS.sss format 
;;			Elapsed Time	D:HH:MM:SS.sss format 
;; 
;;DEPENDENCIES   WMI, TimeDiff() external UDF 
;; 
;;TESTED WITH    W2K, WXP, W2K3, Vista, x64 
;; 
;;EXAMPLES        
; 
Function WMIProcessList(OPTIONAL $_Process, OPTIONAL $_Computer, OPTIONAL $_pAuth, OPTIONAL $_CFlag)
 
  Dim $_objWMIService, $_colItems, $_objItem	; WMI object vars 
  Dim $_Line					; line string 
  Dim $_aTmp[0], $_I				; return array, index 
  Dim $_					; temp var 
  Dim $_BTime, $_CTime				; boot and current times from target system 
 
  $_I = -1
 
  ; insure a properly formatted computer name, default to local computer is not specified 
  $_Computer = IIf(Not $_Computer, '.', Join(Split($_Computer,'\'),''))
 
  ; If a pre-authenticated WMI object pointer was provided, use it, otherwise create a new object pointer 
  If $_pAuth
    $_objWMIService = $_pAuth
  Else
    $_objWMIService = GetObject('winmgmts:{impersonationLevel=impersonate}!\\' + $_Computer + '\root\cimv2')
    If @ERROR Exit Val('&' + Right(DecToHex(@ERROR), 4)) EndIf
  EndIf
 
  ; Get the current and boot times from the client system 
  $_colItems = $_objWMIService.ExecQuery("Select * from Win32_OperatingSystem",,48)
  For each $_objItem in $_colItems
    $_BTime = $_objItem.LastBootUpTime			; host-local boot time 
    $_CTime = $_objItem.LocalDateTime			; host-local current time 
  Next
 
  $_colItems = 0
  ; convert to a normalized time string 
  $_CTime = SubStr($_CTime, 1,4) + '/' + SubStr($_CTime, 5,2) + '/' +SubStr($_CTime, 7,2) + ' '
         + SubStr($_CTime, 9,2) + ':' + SubStr($_CTime, 11,2) + ':' + SubStr($_CTime, 13,6)
 
  ; get the collection of process objects 
  $_colItems = $_objWMIService.ExecQuery("Select * from Win32_Process",,48)
  If @ERROR   $_colItems = 0 Exit Val('&' + Right(DecToHex(@ERROR), 4)) EndIf
 
  ; Enumerate the collection of process data 
  For Each $_objItem in $_colItems
    ; add the data to the array if no process is specified, or if a specific process name or ID is specified 
    If Not $_Process Or $_Process = $_objItem.Name Or $_Process = $_objItem.ProcessID 
      $_Line =  IIf($_CFlag, $_objItem.CommandLine, $_objItem.Name)
      $_Line = $_Line + ',' + $_objItem.ProcessID
      $_Line = $_Line + ',' + $_objItem.Priority
      $_Line = $_Line + ',' + $_objItem.ThreadCount
      $_Line = $_Line + ',' + $_objItem.HandleCount
      $_Line = $_Line + ',' + $_objItem.WorkingSetSize
      ; convert the following to d:hh:mm:ss.sss format 
      $_Line = $_Line + ',' + _WMIPLTC(CDbl($_objItem.UserModeTime) * 0.0000001)
      $_Line = $_Line + ',' + _WMIPLTC(CDbl($_objItem.KernelModeTime) * 0.0000001)
      ; Use the system boot time if creation date is not set 
      $_ = IIf($_objItem.CreationDate , $_objItem.CreationDate, $_BTime)
      ; calculate elapsed time and convert to d:hh:mm:ss.sss format 
      $_Line = $_Line + ',' +  _WMIPLTC(_WMIPLET($_, $_CTime))
      ; Update the array 
      $_I = $_I + 1
      ReDim Preserve $_aTmp[$_I]
      $_aTmp[$_I] = $_Line
    EndIf
  Next
 
  ; return the array, close the collection, and gripe if no items were found 
  $WMIProcessList = $_aTmp
  $_colItems = 0
  If $_Process and $_I < 0 Exit 1911 EndIf
  Exit 0
 
EndFunction
 
 
; support function to calculate elapsed time as Seconds 
; Dependent on TimeDiff UDF! 
Function _WMIPLET($_Time, $_CTime)
 
  Dim $_CurrentTime
 
  ; Break into Date and Time parts, including 3 decimal points 
  $_Time = SubStr($_Time, 1,4) + '/' + SubStr($_Time, 5,2) + '/' +SubStr($_Time, 7,2) + ' '
         + SubStr($_Time, 9,2) + ':' + SubStr($_Time, 11,2) + ':' + SubStr($_Time, 13,6)
 
  ; return the value with 3 decimal places 
  $_WMIPLET = TimeDiff($_Time, $_CTime, '', 1); FormatNumber(, 3, -1, 0, 0) 
  Exit 0
 
EndFunction
 
 
; support function to conver the time value (100ns units) to D:H:M:S.s format 
Function _WMIPLTC($_Units)
 
  Dim $_D, $_H, $_M, $_S		; day, hour, minute, & second values 
 
  ; Find d/h/m and subtract result from units 
  $_D = Int($_Units / 86400)	$_Units = $_Units - $_D * 86400
  $_H = INT($_Units/3600)	$_Units = $_Units - $_H * 3600
  $_M = INT($_Units/60)
  $_S = FormatNumber($_Units - $_M * 60, 3, -1, 0, 0)
 
  ; return a time string 
  $_WMIPLTC = '' + $_D + ':' + $_H + ':' + $_M + ':' + $_S
  Exit 0
 
EndFunction
 
 
 
 
And..

;; 
;;====================================================================== 
;; 
;;FUNCTION       WMIProcessKill() 
;; 
;;ACTION         Terminates a specific process by numeric PID 
;; 
;;AUTHOR         Glenn Barnas 
;; 
;;VERSION        1.0 / 2007/10/20 
;; 
;;SYNTAX         WMIProcessKill(Process [, Computer] [, AuthPtr]) 
;; 
;;PARAMETERS     Process - PID of process to terminate 
;; 
;;		 Computer - OPTIONAL, Name of computer to target 
;; 
;;               AuthPtr - OPTIONAL - pre-authenticated WMI object pointer 
;;                 Use WMIAuthentication() udf to create the AuthPtr value 
;;                 AuthPtr is not needed if user has admin rights 
;; 
;;REMARKS        Terminates the process after verifying it exists 
;; 
;;RETURNS        1 (success) or 0 (failure) 
;; 
;;DEPENDENCIES   WMI 
;; 
;;TESTED WITH    W2K, WXP, W2K3, Vista, x64 
;; 
;;EXAMPLES        
; 
Function WMIProcessKill($_Process, Optional $_Computer, OPTIONAL $_pAuth)
 
  Dim $_objWMIService, $_colItems, $_objItem	; WMI object vars 
  Dim $_					; temp var 
  Dim $_Err					; error code 
 
  ; Must be a single numeric value to terminate 
  If Val($_Process) <> $_Process
    $WMIProcessKill = 0
    Exit 87
  EndIf
 
  ; insure a properly formatted computer name, default to local computer is not specified 
  $_Computer = IIf(Not $_Computer, '.', Join(Split($_Computer,'\'),''))
 
  ; If a pre-authenticated WMI object pointer was provided, use it, otherwise create a new object pointer 
  If $_pAuth
    $_objWMIService = $_pAuth
  Else
    $_objWMIService = GetObject('winmgmts:{impersonationLevel=impersonate}!\\' + $_Computer + '\root\cimv2')
    If @ERROR Exit Val('&' + Right(DecToHex(@ERROR), 4)) EndIf
  EndIf
 
  ; get the collection of process objects  
  $_colItems = $_objWMIService.ExecQuery("Select * from Win32_Process Where ProcessID=" + '"' + $_Process + '"',,48)
  If @ERROR   $_colItems = 0 Exit Val('&' + Right(DecToHex(@ERROR), 4)) EndIf
 
  $_Err = 2	; prepare for not found 
 
  ; Enumerate the collection of process data 
  For Each $_objItem in $_colItems
    $_ = $_objItem.Terminate
    $_Err = @ERROR
  Next
  $_colItems = 0
 
  ; return appropriate values 
  $WMIProcessKill = Not $_Err
  Exit $_Err
 
EndFunction
 
 
 
 
_________________________
Actually I am a Rocket Scientist! \:D

Top
#192759 - 2009-03-09 11:33 PM Re: check if program is running [Re: Glenn Barnas]
jeff_eisenberg Offline
Fresh Scripter

Registered: 2009-02-26
Posts: 45
Loc: CA
Thank you very much.

Any chance this line is bad??

$_WMIPLET = TimeDiff($_Time, $_CTime, '', 1); FormatNumber(, 3, -1, 0, 0)

I get line 132 expected ')'

But it seems ok.

Jeff.

Top
#192761 - 2009-03-09 11:37 PM Re: check if program is running [Re: jeff_eisenberg]
jeff_eisenberg Offline
Fresh Scripter

Registered: 2009-02-26
Posts: 45
Loc: CA
Actually, I take that back.

I only get that message when I call the function:

$ie = WMIProcessList('iexplore.exe')
MESSAGEBOX ($ie,"Notice")

Any ideas?

I'm calling your script, Call "WMIProcessList.KIX" in my script. That seems to run fine. But when I run the two lines above I get the line 132 in WMIProcessList.kix expected ')'

Top
#192762 - 2009-03-10 12:01 AM Re: check if program is running [Re: jeff_eisenberg]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
Jeff,

Doh! I was rushing to get out the door.. the first UDF has a dependency on the TimeDiff UDF.

My web site's back up - you can grab the latest copy of TimeDiff from there - Resources / Kix Function Library. Insert that in your script and you should be fine.

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

Top
#192764 - 2009-03-10 03:37 AM Re: check if program is running [Re: Glenn Barnas]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
BTW, Jeff..

The UDFs that are published here have a pretty standardized header - sort of a mini-manual for the function. The important thing (which I overlooked today) is the Dependencies list, which identifies special needs of the UDF, whether they be other UDFs, external programs, or even DLLs needing to be registered.

Regarding your error message, try this:
 Code:
$Val = BadFunc()
'Val is ' $Val ?
Then try
 Code:
$Val = BadFunc('SomeData')
'Val is ' $Val ?

Do you see what's happening? The result is that "BadFunc" is treated as an unquoted string and simply stored in the variable. The parenthesis are used to group what is now either nothing (ex 1) or a string (ex 2), which is simply displayed on the console. This is a simple example to illustrate the result of a missing UDF... your earlier problem had a much more complex syntax, and I'd wager that things were fine until the interpreter encountered the comma and thought "hey! what's this doing here, and where is the ')' to close the opening '(' from back there?"... of course, what it thought was different from what it said - "line 132 expected')'". ;\) You can prove this logic by supplying a second arg to BadFunc in example 2.

I don't know your kix or scripting experience, but this is a fairly common mistake that many can learn from.. I often give my class a page full of bad commands so they can recognize what's wrong from the often cryptic messages.

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

Top
#192771 - 2009-03-10 12:27 PM Re: check if program is running [Re: Glenn Barnas]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
one more thing, there is a thing called "search" in the board menu.
if you say "enumprocess" you will get to the udf.
if you look in to google with "enumprocess()", it's the 4th hit.
if you search for "enumprocess udf", it's the first.

so not really sure you even looked around.

also, enumprocess() is able to kill the process as well, so you would get 2 of those above functions in one ;\)
_________________________
!

download KiXnet

Top
#192781 - 2009-03-10 03:44 PM Re: check if program is running [Re: Lonkero]
jeff_eisenberg Offline
Fresh Scripter

Registered: 2009-02-26
Posts: 45
Loc: CA
Actually, I looked I just didn't realize that that fucntion was what I needed to use. Now it working using the short simple piece of code. I'm new at this so I catch on a littel slow.

I thought that was an example or somehting, not the code you need to use. I'm up and running now. I didn't realize it was that simple.

htnaks,

JEff.

Top
#192785 - 2009-03-10 04:04 PM Re: check if program is running [Re: jeff_eisenberg]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
Great!

It doesn't matter how fast or slow that you catch on.. just that you do. \:\)

BTW, Lonk - the two functions I provided were designed to specifically mimic PSList.exe and PSKill.exe, which is why they were separate files.

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 778 anonymous users online.
Newest Members
batdk82, StuTheCoder, M_Moore, BeeEm, min_seow
17885 Registered Users

Generated in 0.064 seconds in which 0.024 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