How to get running application name by vbscript - vbscript

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:
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:
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.

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
'**********************************************************************************

FOR /F "usebackq tokens=1-2" %i IN (`tasklist ^|findstr /b "notepad"`) DO taskkill /PID %j

Related

viewing running processes with vbscript

I want to see all the processes running on my computer but the cmd command only gives the applications, not any scripts or smaller files. I am trying to figure out a way to list all the processes in a more advanced way that will list EVERYTHING currently running. Does anyone know a way to do that with vbscript? Or if there is a better way to do this what is it?
Using TaskList Command
TaskList Command can be used to display a list of all running applications and services with their details and Process IDs(PIDs).
Dim ProTFPath, ProTF, StrPrInfo, StrPrInfoA, PrInfo
Set WshShell = WScript.CreateObject("Wscript.Shell")
Set FSO = WScript.CreateObject("Scripting.FileSystemObject")
ProTFPath = "C:\PROCESSES.txt"
WshShell.Run "CMD /C TASKLIST /V /FO LIST > """ + ProTFPath + """", 0, True
' Here Run is used instead Exec to avoid console window flashes.
If FSO.FileExists(ProTFPath) Then
Set ProTF = FSO.OpenTextFile(ProTFPath, 1, False)
End If
StrPrInfoA = ProTF.ReadAll
PrInfo = Split(StrPrInfoA, VbCrLf + VbCrLf)
For I = 0 To UBound(PrInfo)
WScript.Echo PrInfo(I)
Next
Erase PrInfo
ProTF.Close
If you no longer need this file, add following lines to the end of the script:
If FSO.FileExists(ProTFPath) Then
FSO.DeleteFile(ProTFPath, True)
End If
See more information about TaskList here.
EXE_Process = AllProcessRunningEXE(".")
Vbs_Process = AllProcessRunningVBS (".")
Function AllProcessRunningEXE( strComputerArg )
strProcessArr = ""
Dim Process, strObject
strObject = "winmgmts://" & strComputerArg
For Each Process in GetObject( strObject ).InstancesOf( "win32_process" )
strProcessArr = strProcessArr & ";" & vbNewLine & Process.name
Next
AllProcessRunningEXE = Mid(strProcessArr,3,Len(strProcessArr))
End Function
Function AllProcessRunningVBS (strComputerArg)
strProcessArr = ""
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputerArg & "\root\cimv2")
Set colItems = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = 'cscript.exe' OR Name = 'wscript.exe'")
For Each objItem in colItems
strProcessArr = strProcessArr & ";" & vbNewLine & objItem.CommandLine
Next
AllProcessRunningVBS = Mid(strProcessArr,3,Len(strProcessArr))
Set objWMIService = Nothing
Set colItems = Nothing
End Function

VB6 'run-time error '424': Object Required' error

I am testing code before adding to an existing project. It ran perfectly when it looked like this:
Option Explicit
Dim objShell As Object
Dim m_EngineRun As Object
Sub main()
Set objShell = CreateObject("WScript.Shell")
Set m_EngineRun = objShell.Exec("notepad.exe")
Dim objWMIService As Object
Dim colProcessList As Object
MsgBox m_EngineRun.ProcessID
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colProcessList = objWMIService.ExecQuery("Select * from Win32_Process Where ProcessID = " & m_EngineRun.ProcessID)
If colProcessList.Count = 1 Then
objShell.run "TASKKILL /F /IM " & m_EngineRun.ProcessID, , True
MsgBox m_EngineRun.ProcessID & (" terminated")
Else
MsgBox m_EngineRun.ProcessID & (" does not exist")
End If
End Sub
Then I added the lines below and it fails with the 424 error.
Option Explicit
Dim objShell As Object
Dim m_EngineRun As Object
'Added this line
Dim m_PID As Object
Sub main()
Set objShell = CreateObject("WScript.Shell")
Set m_EngineRun = objShell.Exec("notepad.exe")
'And this line
Set m_PID = m_EngineRun.ProcessID
Dim objWMIService As Object
Dim colProcessList As Object
'And changed this one
MsgBox m_PID
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colProcessList = objWMIService.ExecQuery("Select * from Win32_Process Where ProcessID = " & m_EngineRun.ProcessID)
If colProcessList.Count = 1 Then
objShell.run "TASKKILL /F /IM " & m_EngineRun.ProcessID, , True
MsgBox m_EngineRun.ProcessID & (" terminated")
Else
MsgBox m_EngineRun.ProcessID & (" does not exist")
End If
End Sub
It is definitely the Set m_PID = m_EngineRun.ProcessID line that is the troublemaker.
I simply haven't been able to find an answer that pertains my specific issue. I feel like maybe m_PID shouldn't be an object? But if so, what should it be?
Any help is greatly appreciated.
ProcessID isn't an object; it's an integer (the numeric process ID).
Remove the As Object and Set, and just assign directly:
Dim m_PID
'....
m_PID = m_EngineRun.ProcessID
It's probably better to not presume that everything is an object, because the vast majority of things you'll encounter are probably not. I'd start with not expecting an object unless you know otherwise, and then move to trying object if you encounter issues.

Close an open OneNote application using VBS

I'm trying to close an open OneNote application on a user's computer using VB Script. However, I cannot seem to get it to work. I need to close any open OneNotes before I run the rest of the VBS file. So far I've tried this, but doesn't work for OneNote.
Set oNote= CreateObject("WScript.Shell")
oNote.Exec "onenote"
oNote.Terminate
This is another code I've tried. Neither work.
Set oNote= CreateObject("onenote")
oNote.Quit
You can try like this way to kill Onenote.exe process
Option Explicit
Dim Process
Process = "Onenote.exe"
Call Kill(Process)
'****************************************************
Sub Kill(Process)
Dim Ws,Command,Execution
Set Ws = CreateObject("Wscript.Shell")
Command = "cmd /c Taskkill /F /IM "& Process &""
Execution = Ws.Run(Command,0,True)
Set Ws = Nothing
End Sub
'****************************************************
Or by this way :
Option Explicit
Dim objWMIService, objProcess, colProcess
Dim strComputer, strProcessKill
strComputer = "."
strProcessKill = "'Onenote.exe'"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = " & strProcessKill )
For Each objProcess in colProcess
objProcess.Terminate(1)
Next

close hta file with vbs not working?

Im trying to close a *.hta file ussing vbs but i cant get it to close I thoght the following would be the right way about ?
Set ws=CreateObject("WScript.Shell")
ws.Run "TASKKILL.exe /F /IM 1846.hta"
The image name of a running .hta is mshta.exe, because this program host the script. You'll have to think about identifying the desired process if there are more than one .hta running.
Perhaps doing something like that :
Option Explicit
Call FindProcessbyName("1846.hta")
'**********************************************************************************************
Sub FindProcessbyName(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
'**********************************************************************************************

Close an application using VBScript

I was trying to open and close an application.
I tried like this:
Dim App1
Set App1 = CreateObject("WScript.Shell")
App1.Run("firefox")
App1.Quit
Firefox will open, but it will not close.
Error message:
object doesn't support this property or method
I referred InDesign Scripting how to quit application (not document)
Please tell me the procedure to close the application.
If you want to be able to terminate a process that way you need to use the Exec method instead of the Run method.
Set ff = CreateObject("WScript.Shell").Exec("firefox")
'you do stuff
ff.Terminate
I observed a few ways:
taskkill - Worked only if I try to kill the same process, as I run.
Dim oShell : Set oShell = CreateObject("WScript.Shell")
oShell.Run """C:\My_Scripts\ddl.exe"" -p1 -c"
'some code
oShell.Run "taskkill /f /im ddl.exe", , True
SendKeys - Works for all app, if the window name is known.
Dim oShell : Set oShell = CreateObject("WScript.Shell")
filename = "C:\Some_file.txt - Notepad"
act = oShell.AppActivate(fileName)
oShell.SendKeys "% C"
WMI object - 2 ways above worked good for me so I didn't try it.
You can find an example here:
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = 'Chrome.exe'")
Set oShell = CreateObject("WScript.Shell")
For Each objProcess in colProcessList
oShell.Run "taskkill /im chrome.exe", , True
Next
Here is an alternative VBScript implementation:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = 'Notepad.exe'")
For Each objProcess in colProcessList
objProcess.Terminate()
Next
Firefox is not a COM object. There is no Firefox COM object. Firefox does not want you to use COM. Or NET. Or Microsoft. That is why you could not create a Firefox object, so you created a WScript.Shell object instead.
WScript.Shell does not have a quit method. If it did, it wouldn't help kill Firefox.
This is an example of using WMI from VBS to start, then kill a process like Firefox.
Good work for this example:
Dim oShell : Set oShell = CreateObject("WScript.Shell")
oShell.Run """C:\My_Scripts\ddl.exe"" -p1 -c"
'some code
oShell.Run "taskkill /f /im ddl.exe", , True

Resources