Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I kill a process, using VBScript, started by a particular user

I have multiple users running attachemate on a Windows 2003 server. I want to kill attachemate.exe started by user_1 without killing attachemate.exe started by user_2.

I want to use VBScript.

like image 612
GlennH Avatar asked Sep 16 '08 19:09

GlennH


2 Answers

You could use this to find out who the process owner is, then once you have that you can use Win32_Process to kill the process by the process ID.

MSDN Win32_Process class details

MSDN Terminating a process with Win32_Process

There is surely a cleaner way to do this, but here's what I came up with. NOTE: This doesn't deal with multiple processes of the same name of course, but I figure you can work that part out with an array to hold them or something like that. :)

strComputer = "."
strOwner = "A111111"
strProcess = "'notepad.exe'"

' Connect to WMI service and Win32_Process filtering by name'
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")
Set colProcessbyName = objWMIService.ExecQuery("Select * from Win32_Process Where Name = " _
    & strProcess)

' Get the process ID for the process started by the user in question'
For Each objProcess in colProcessbyName
    colProperties = objProcess.GetOwner(strUsername,strUserDomain)
    if strUsername = strOwner then
        strProcessID = objProcess.ProcessId
    end if
next

' We have the process ID for the app in question for the user, now we kill it'
Set colProcessList = objWMIService.ExecQuery("Select * from Win32_Process where ProcessId =" & strProcessID)
For Each objProcess in colProcess
    objProcess.Terminate()
Next
like image 125
unrealtrip Avatar answered Sep 29 '22 19:09

unrealtrip


Shell out to pskill from http://sysinternals.com/

Commandline: pskill -u user_1 attachemate.exe

like image 33
Colin Neller Avatar answered Sep 29 '22 20:09

Colin Neller