Код:
UserName = "ThisUser" 'Имя конкретного пользователя
NProcMax = 3 'Максимальное количество одноименных процессов. Если больше - будут удалены все
sQuery = "Select *From Win32_Process"
Set Processes = GetObject("winMgmts:").ExecQuery(sQuery)
NProc = Processes.Count
ReDim AllProc(NProc, 2)
MProc = 0
For Each Process In Processes
If Process.GetOwner(User) = 0 Then
If LCase(User) = LCase(UserName) Then
MProc = MProc + 1
AllProc(MProc, 1) = Process.Name + "*" + User
AllProc(MProc, 2) = 0
End If
End If
Next
i = 1
Do
i0 = i
AllProc(i0, 2) = 1
If i = MProc Then Exit Do
For j = i + 1 To NProc
If AllProc(i, 1) = AllProc(j, 1) Then
i = i + 1
s = AllProc(i, 1)
AllProc(i, 1) = AllProc(j, 1)
AllProc(j, 1) = s
AllProc(i0, 2) = AllProc(i0, 2) + 1
End If
Next
i = i + 1
If i > MProc Then Exit Do
Loop
For i = 1 To MProc
NDelProc = AllProc(i, 2) - NProcMax
If NDelProc > 0 Then
Program = Mid(AllProc(i, 1), 1, InStr(1, AllProc(i, 1), "*") - 1)
sQuery = "Select *From Win32_Process Where Name=" + """" + Program + """"
Set Processes = GetObject("winMgmts:").ExecQuery(sQuery)
For Each Process In Processes
If Process.GetOwner(User) = 0 Then
If LCase(User) = LCase(UserName) Then Process.Terminate
' If NDelProc > 0 Then
' Process.Terminate
' NDelProc = NDelProc - 1
' End If
'End If
End If
Next
End If
Next