Monday, February 7, 2011

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.

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

    Commandline: pskill -u user_1 attachemate.exe

    GlennH : Needing to install pskill is not ideal. I'd prefer a solution that does not require me to install anything new.
  • 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
    
    From unrealtrip

0 comments:

Post a Comment