Ahhh, thanks for putting the final finishing to the code. I will definitely put that in.

Edit:

I found out that the %TEMP% environment variable returns letters for session_ids greater than 9. Therefore, session_id '10' is 'a', etc.

i.e. C:\Documents and Settings\[username]\Local Settings\Temp\a

So I have modified the function, GetSessionID with the following (I know everyone here likes one-liner functions, but I couldn't do it with this one! Call me a traditional coder! )

Code:

Function GetSessionID
Dim $s_sessionid

$s_sessionid = SubStr(%TEMP%,1+InStrRev(%TEMP%,"\"))
If $s_sessionid <> CInt($s_sessionid)
If Len($s_sessionid) = 1
$s_sessionid = CStr(Asc(Ucase($s_sessionid))-55)
Else
$s_sessionid = CStr(CInt($s_sessionid))
EndIf
EndIf

$GetSessionID = $s_sessionid
EndFunction


Code:

Function EnumProcess($exe, optional $terminate, optional $Computer)
Dim $winmgmts, $ExecQuery, $Process, $id, $sessionid
Dim $GetObject
If NOT $computer $computer=@wksta EndIf
$winmgmts="winmgmts:{impersonationLevel=impersonate}!//$COMPUTER"
$sessionid = GetSessionID
Select
Case Val($exe)>0
$ExecQuery="select * from Win32_Process where ProcessId='$exe' and SessionId='$sessionid'"
$GetObject=GetObject($winmgmts).ExecQuery($ExecQuery)
For Each $Process in $GetObject
If $terminate $=$Process.Terminate EndIf
$EnumProcess = $Process.name
Next
$GetObject=''
Case VarType($exe)=8
$ExecQuery="select * from Win32_Process where Name='$exe' and SessionId='$sessionid'"
$GetObject=GetObject($winmgmts).ExecQuery($ExecQuery)
For Each $Process in $GetObject
If $terminate $=$Process.Terminate EndIf
$id=$Process.ProcessId
$EnumProcess = "$Id" + "|" + "$EnumProcess"
Next
$EnumProcess=Left($EnumProcess,Len($EnumProcess)-1)
$GetObject=''
Case 1
Exit 1
EndSelect
EndFunction



Edited by pearly (2005-02-09 01:32 AM)