代码之家  ›  专栏  ›  技术社区  ›  GlennH

如何使用VBScript终止由特定用户启动的进程

  •  6
  • GlennH  · 技术社区  · 16 年前

    我有多个用户在Windows 2003服务器上运行attachchemate。我想杀死由user_1启动的attchemate.exe,而不杀死由user_2启动的attSchemate.exe。

    我想用VBScript。

    2 回复  |  直到 16 年前
        1
  •  5
  •   unrealtrip    16 年前

    您可以使用它来找出进程所有者是谁,然后一旦您有了,您就可以使用Win32_process通过进程ID杀死该进程。

    MSDN Win32_Process class details

    MSDN Terminating a process with Win32_Process

    当然有一种更干净的方法可以做到这一点,但这是我想到的。注意:当然,这并不涉及同名的多个进程,但我认为你可以用一个数组来保存它们或类似的东西来解决这部分问题。 :)

    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
    
        2
  •  2
  •   Colin Neller    16 年前

    壳牌出来pskill从 http://sysinternals.com/

    命令行:pskill-u user_1 attachchemate.exe