如何使用VBScript杀死由特定用户启动的进程

时间:2008-09-16 19:58:05

标签: vbscript windows-server-2003 kill

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

我想使用VBScript。

2 个答案:

答案 0 :(得分:5)

您可以使用它来查找进程所有者是谁,然后一旦有了,您可以使用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

答案 1 :(得分:2)

http://sysinternals.com/

打包到pskill

命令行:pskill -u user_1 attachemate.exe