Ronnie Gurr Ronnie Gurr - 1 year ago 77 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
End If
End Sub

Answer Source

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))
OutPut.WriteLine String(50,"*") 
OutPut.WriteLine count & " Process were killed !"
OutPut.WriteLine String(50,"*")
If fso.FileExists(LogFile) Then 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)
End Sub
Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download