2

I would like to get the name list of running application by vbscript and kill the application by its main window title. Those applications should be listed on Task Manager -> Applications tab

Like this:
enter image description here
After searching from web, I found vbscript like this:

'FUNCTION
Function ListProcessRunning()
'This function can report names from
'TaskManager -> Processes
    sComputerName = "."
    Set objWMIService = GetObject("winmgmts:\\" & sComputerName & "\root\cimv2")
    sQuery = "SELECT * FROM Win32_Process"
    Set objItems = objWMIService.ExecQuery(sQuery)
    'iterate all item(s)
    For Each objItem In objItems
        WScript.Echo objItem.Name
    Next
End Function

This vbscript list all process names which are under Task Manager -> Processes tag like this:
enter image description here
Which is not what I want.

I also found this:

'FUNCTION
Function ListApplicationRunning()
'This function can report names from
'TaskManager -> Application

    Set Word = CreateObject("word.application") 
    For Each x In Word.Tasks
        WScript.Echo x.Name
    Next 
    Word.Quit 
    Set Word = Nothing 
End Function

Which really give me what I want but the problem is the server I am going to run this script has no Word so no word.application for vbscript and I am not able to install one for it.

My question is how to get the application name and kill the application by that name? I am not sure is it possible to do with vbscript only, may be a combination of vbscript and cmd is also okay.

sflee
  • 1,659
  • 5
  • 32
  • 63
  • 1
    See if [this](http://stackoverflow.com/a/20398390/2861476) solves your problem. – MC ND Dec 12 '16 at 09:48
  • sorry, your script will give me the list of process, which is not what I want. Thank you very much. – sflee Dec 14 '16 at 06:28
  • 1
    The indicated code allows you to retrieve the title of the window (the last column in tasklist output or `task(8)` in the code). Now you can use the PID (`task(1)`) in a `taskkill` or locate the process via `WMI` query and call the `Terminate` method of the process. – MC ND Dec 14 '16 at 06:45
  • @MCND I see what you means, but my application cannot list by `tasklist `. I finally used c++ to finish my task. Thank you very much. – sflee Dec 17 '16 at 11:49

2 Answers2

1

In vbscript we can do something like that just give a try with it :

Option Explicit
Call KillProcessbyName("common-api.jar")
'*********************************************************************************
Sub KillProcessbyName(FileName)
    On Error Resume Next
    Dim WshShell,strComputer,objWMIService,colProcesses,objProcess
    Set WshShell = CreateObject("Wscript.Shell")
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colProcesses = objWMIService.ExecQuery("SELECT * FROM Win32_Process")
    For Each objProcess in colProcesses
        If InStr(objProcess.CommandLine,FileName) > 0 Then
            If Err <> 0 Then
                MsgBox Err.Description,VbCritical,Err.Description
            Else
                objProcess.Terminate(0) 
            End if
        End If
    Next
End Sub
'**********************************************************************************
Hackoo
  • 18,337
  • 3
  • 40
  • 70
  • Thank you I have tried the script but this script give me the list of processes, which is not I want. – sflee Dec 14 '16 at 06:33
1
FOR /F "usebackq tokens=1-2" %i IN (`tasklist ^|findstr /b "notepad"`) DO taskkill /PID %j