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?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.