Page 1 of 1 1
Topic Options
#173377 - 2007-01-29 10:27 PM VBS code
jon Offline
Fresh Scripter

Registered: 2007-01-15
Posts: 11
Can you detect a process based on the currently logged in user?

I'm calling this vbs script from kixtart and can view if a process is running and stop the script if it's there but I need to go even further to find out if the current user has the specific process running.

This is due to the script running on a terminal server where other users may have the process runnning but not under the current logged in user.

Code:
Function IsOutlookRunning()
    strComputer = "."
    strQuery = "Select * from Win32_Process " & _
               "Where Name = 'Outlook.exe'"
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" _
        & strComputer & "\root\cimv2")
    Set colProcesses = objWMIService.ExecQuery(strQuery)
    For Each objProcess In colProcesses
       If UCase(objProcess.Name) = "OUTLOOK.EXE" Then
            IsOutlookRunning = True
        Else
           IsOutlookRunning = False
        End If
    Next
End Function

I believe the below to be the code I require but I am having trouble incorporating it into the above. Can someone please help me out?

Code:
 
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_Process")
For Each objProcess in colProcessList
 colProperties = objProcess.GetOwner(strNameOfUser,strUserDomain)
 Wscript.Echo "Process " & objProcess.Name & " is owned by " _
 & strUserDomain & "\" & strNameOfUser & "."
Next


Top
#173378 - 2007-01-29 11:03 PM Re: VBS code [Re: jon]
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11634
Loc: Space
No KiXtart does not support the OUT method required to determine WHO is running the process. You will need to use VBS to do this as you're doing now.

Basically get the user name on the process then kill it with WMI using the process ID or if possible the owner.

You can look on Microsoft's site for further information on WMI code samples they also have a download of their scripts in a CHM file that makes it easy to locate specific code.

Top
#173407 - 2007-01-30 11:27 AM Re: VBS code [Re: NTDOC]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
True, KiXtart does not support OUT parameters which is a problem.

However in this case there is a work-around (shamelessly stolen some time ago from elsewhere on this board - apologies to the original author)

Code:
$strComputer = "."
$processes = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" + $strComputer + "\root\cimv2").ExecQuery("Select * from Win32_Process")
FOR EACH $process IN $processes
 
	$ProcessDomain = $process.ExecMethod_("GetOwner").Domain
	$ProcessUser = $process.ExecMethod_("GetOwner").User
 
	$process.Name+":	"+$ProcessDomain+"\"+$ProcessUser+@CRLF
NEXT
; vim600:ts=4 sw=4 ai fdc=4 fdm=marker

Top
#173432 - 2007-01-30 07:31 PM Re: VBS code [Re: Richard H.]
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11634
Loc: Space
Thanks Richard this will be a nice addition to another script
Top
Page 1 of 1 1


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

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

Generated in 0.063 seconds in which 0.035 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