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

时间:2020-03-05 18:57:00  来源:igfitidea点击:

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

我想使用VBScript。

解决方案

回答

从http://sysinternals.com/购买pskill

命令行:pskill -u user_1 attachemate.exe

回答

我们可以使用它来找出进程所有者是谁,然后一旦拥有,便可以使用Win32_Process通过进程ID杀死该进程。

MSDN Win32_Process类的详细信息

MSDN使用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