Ronnie Gurr Ronnie Gurr - 27 days ago 6
Vb.net Question

Killing Two different Processes With VB

I am trying to figure out how to kill two processes at the same time I have managed to get one to work when it is opened but the other wont close.

Sub block()
For Each item As Process In Process.GetProcesses
If item.ProcessName = "taskmgr" And item.ProcessName = "cmd" Then
item.Kill()
End If
Next
End Sub

Answer

Give a try with this solution in vbscript :

Option Explicit
Dim Ws,fso,MainArray,LogFile,i,OutPut,count
Set Ws = CreateObject("Wscript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
MainArray = Array("taskmgr.exe","cmd.exe")
LogFile = Left(Wscript.ScriptFullName,InstrRev(Wscript.ScriptFullName, ".")) & "log"
count = 0 
If fso.FileExists(LogFile) Then fso.DeleteFile LogFile
Set OutPut = fso.OpenTextFile(LogFile,2,True)
For i = LBound(MainArray) To UBound(MainArray)
    Call Kill(MainArray(i))
Next
OutPut.WriteLine String(50,"*") 
OutPut.WriteLine count & " Process were killed !"
OutPut.WriteLine String(50,"*")
If fso.FileExists(LogFile) Then
    ws.run DblQuote(LogFile) 'To show the LogFile
End if
'******************************************
Sub Kill(MyProcess)
    Dim colItems,objItem
    Set colItems = GetObject("winmgmts:").ExecQuery("Select * from Win32_Process " _
    & "Where Name like '%"& MyProcess &"%' AND NOT commandline like '%" & wsh.scriptname & "%'",,48)
    For Each objItem in colItems
        count= count + 1
        OutPut.WriteLine Mid(objItem.CommandLine,InStr(objItem.CommandLine,""" """) + 2)
        objItem.Terminate(0)
    Next
End Sub
'***********************************************
Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function
'***********************************************