How to kill specific HTA based on window title - vbscript

I have an HTA file with the filepath: C:\Users\ME\Desktop\DataTable.hta which has a window title of DataTable as declared in its code using <title>DataTable</title>
I'm trying to close this specific HTA window using DOS, javascript or vbscript. however, when I try to use taskkill in the following fashion its doesn't close. It works fine for notepad and other windows, but not for HTAs.
I type this into dos:
taskkill /FI "WINDOWTITLE eq DataTable
and nothing happens. Yet if I use:
taskkill /FI "WINDOWTITLE eq Untitled - Notepad
it successfully closes notepad. Why won't it work for HTAs? Is there a solution?
Thank you.

We presume that you are running a HTA with this name : DataTable.hta.
So,we can kill this HTA by its name using a vbscript like that :
Option Explicit
Call KillProcessbyName("DataTable.hta")
'**********************************************************************************************
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(UCase(objProcess.CommandLine),UCase(FileName)) > 0 Then
If Err <> 0 Then
MsgBox Err.Description,VbCritical,Err.Description
Else
objProcess.Terminate(0)
End if
End If
Next
End Sub
'**********************************************************************************************

Related

How to stop a specific script in VBScript

Is there a code to stop a specific script? I've seen a way that allows you to end all scripts using taskkill.
Option Explicit
Dim WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "taskkill /f /im Cscript.exe", , True
WshShell.Run "taskkill /f /im wscript.exe", , True
I simply want to end one specific script that is sleeping in a wscript.sleep not every script that is running at a time.
Try below. You need to preserve the ProcessId of the process you want to kill. Below code opens a notepad and kills it. This example shows you how to capture the ProcessId of the script when it was started and then kill it.
Private Sub KillTest()
Dim killCmd
Dim pid
Set WshShell = CreateObject("WScript.Shell")
Set EngineRun = WshShell.Exec("notepad")
pid = EngineRun.ProcessID
killCmd = "taskkill /pid " & CStr(pid)
Set EngineRun = WshShell.Exec(killCmd)
Set EngineRun = Nothing
Set WshShell = Nothing
End Sub

How to get running application name by 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

Use VBS to run PowerShell

So I have a .ps1 file which creates a form.
That Form takes 10-20secs depending on PCs performance and connection on first load.
Now I am currently using VBS to load a simple .gif file as a loading screen concurrently running the .ps1 file right after.
My issue at the moment is that, I want to close the loading screen when the form pops up. I tried to determine via processes but that failed because of the it loads powershell.exe but the form takes 10sec...
Is it this possible?
Of have you guys got a better idea to do this?
Dim i
Dim strComputer
Dim FindProc
Dim counter
counter = 0
strComputer = "."
FindProc = "powershell.exe"
'Load the gif file
Set objExplorer = CreateObject("InternetExplorer.Application")
With objExplorer
.Navigate "about:blank"
.Visible = 1
.Document.Title = "Show Image"
.Toolbar=False
.Statusbar=False
.Top=400
.Left=400
.Height=355
.Width=435
.Document.Body.InnerHTML = "<img src='\\10.10.67.173\Templates$\Scripts\Resources\loadingtest.gif'>"
End With
'Run the PS script
Set objShell = CreateObject("Wscript.shell")
objShell.Run "CMD /C START /B " & objShell.ExpandEnvironmentStrings("%SystemRoot%") & "\System32\WindowsPowerShell\v1.0\powershell.exe -executionpolicy bypass -file \\10.10.67.173\Templates$\Scripts\FormSignature-V0.9.5.ps1", 0, False
'Determine when to close Loading screen
Do While counter < 3
wscript.Sleep 2000
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select Name from Win32_Process WHERE Name LIKE '" & FindProc & "%'")
If colProcessList.count>0 then
'Quit the process if it finds its running
WScript.Echo("found")
'objExplorer.quit
else
'Do nothing
WScript.Echo("not found")
End if
Set objWMIService = Nothing
Set colProcessList = Nothing
counter = counter + 1
Loop
Set objShell = Nothing
In your VB script:
Create a file in an folder the PS script can access.
Launch your loading image.
Launch your PS script.
In your wait loop, every second or so, check if the file still exists.
If it does, close the instance of IE and exit your script.
In your PS script:
After your form initialization code is finished or the first action after the form load, locate and delete the file.

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