• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1000
  • Last Modified:

VBS Query PID of specific users process

I need a VBS script to query for a PID of a process based on username and process name.
I only need the WQL statement. I was thinking something like:
SELECT * from Win32_Process Where Name='name' AND Where UUID='something something'

Now theres a Name property there for the process name, but there is nothing that identifies the owner of the process. I don't want to find all processes on a WTS for a given processname, only for a specific user.

Anyone with some help on this?
2 Solutions
Looking at the MSDN page for Win32_Process ( http://msdn.microsoft.com/en-us/library/aa394372%28v=vs.85%29.aspx ) it seems that your cant get the username directly in the WQL, you have to use the .GetOwner method.

From your description of your problem, it seems that you have the PID.  This will be unique so you dont need to filter on an individual user anyway.

I've modified the sample code on the MSDN page.

Let me know if I've got the wrong end of the stick.

strComputer = "." 
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

iProcessID = 3460
Set colProcesses = objWMIService.ExecQuery("select * from win32_process where ProcessID = " & iProcessID)
For Each objProcess in colProcesses
    If objProcess.GetOwner ( User, Domain ) = 0 Then
          Wscript.Echo "Process " & objProcess.Caption & " belongs to " & Domain & "\" & User
          Wscript.Echo "Problem " & Rtn & " getting the owner for process " & objProcess.Caption
    End If

Open in new window

Alan is right, you can use similar to code to that, which I have pasted below, to iterate through every process of a specific name and only display the process if it's the specified process name, and user.


strComputer = "." 
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
strProcessName = "excel.exe"
strUserName = "rsampson"
Set colProcesses = objWMIService.ExecQuery("Select * From Win32_Process Where Name = '" & strProcessName & "'")
For Each objProcess In colProcesses
	intReturn = objProcess.GetOwner(strUser, strDomain)
	If intReturn = 0 Then
		If LCase(strUser) = LCase(strUserName) Then WScript.Echo strDomain & "\" & strUser & " is running " & strProcessName
		'Wscript.Echo "Error getting the owner for process " & strProcessName & ". Return code " & intReturn
	End If

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now