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

Gle*_*nnH 5 vbscript kill windows-server-2003

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

我想使用VBScript.

unr*_*rip 5

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