How to stop a specific script in VBScript - 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

Related

Calling a executable from VBScript is not showing any response [duplicate]

I can run successfully test.vbs with syntax:
Dim WshShell
Set WshShell = CreateObject("WScript.Shell")
sEXE = """\\uncpath\file.exe"""
with CreateObject("WScript.Shell")
.Run sEXE & " ", 1, true ' Wait for finish or False to not wait
end with
however I want to store the output to \\\uncpath\%computername%.txt
This doesn't work:
sEXE = """\\uncpath\file.exe>>\\uncpath\%computername%.txt"""
with CreateObject("WScript.Shell")
.Run sEXE & " ", 1, true ' Wait for finish or False to not wait
end with
error with line: with CreateObject("WScript.Shell")
This doesn't work either.
sEXE = """\\uncpath\file.exe"""
with CreateObject("WScript.Shell")
.Run sEXE & " >>\\uncpath\%computername%.txt", 1, true ' Wait for finish or False to not wait
end with
any help?
The .Run() method doesn't have the ability to read the standard output from a task you use .Exec() for that, but you need a few changes to simulate the blocking that .Run() does for you automatically.
Dim WshShell, sEXE, cmd, result
Set WshShell = CreateObject("WScript.Shell")
sEXE = """\\uncpath\file.exe"""
With CreateObject("WScript.Shell")
Set cmd = .Exec(sEXE)
'Block until complete.
Do While cmd.Status <> 1
WScript.Sleep 100
Loop
'Get output
result = cmd.StdOut.Readall()
'Check the output
WScript.Echo result
Set cmd = Nothing
End With
The other approach is to prefix the sEXE variable so you are using cmd /c (as the >> command is part of that).
This should work
sEXE = "cmd /c ""\\uncpath\file.exe >> \\uncpath\%computername%.txt"""
With CreateObject("WScript.Shell")
.Run sEXE & " ", 1, true ' Wait for finish or False to not wait
End With
Useful Links
WshScriptExec Object (Returned by .Exec())
TextStream Object (Returned by .StdIn, StdOut and StdErr)

VBScript to open, run and close Tor browser

Please edit this VBScript to open, run and close Tor browser:
Dim objShell
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Exec("""C:\Users\Name\Desktop\Tor\Tor Browser\Browser\Tor.exe""/http://www.google.com")
Set objShell = Nothing
WScript.Sleep 20000
Set objShell = objshell.Exec("taskkill /fi ""imagename eq Tor.exe""")
It opens and runs, but doesn't close.
You should try something like this :
Dim objShell
Set objShell = CreateObject("WScript.Shell")
objShell.Run "Taskkill /F /IM Tor.exe",0,True

Issues trying to close a bat file from a vbs script

i'm trying to help my little brother with a vbs script file, i've never used vbs, and i'm having serious issues on finding out how to end a bat file that i've opened with the vbs script after 2 seconds
I've tried terminate but it doesn't work, even running another shell with taskkill and the name of process but nothing
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "C:\\Users\me\Desktop\Samples\t.bat"
Wscript.Sleep 2000`
I would like the bat file to close itself after 2 seconds
Use the Exec command instead of Run.
https://ss64.com/vb/exec.html
"Unlike .Run method, .Exec returns an object which returns additional information about the process started."
This example uses cmd.exe /k (the /k will keep the cmd.exe window open, which will be killed after your 2 second timeout even if your bat script logic finishes before that)
Dim shll : Set shll = CreateObject("WScript.Shell")
Set Rt = shll.Exec("cmd.exe /k C:\Temp\test.bat") : wscript.sleep 2000 :
Rt.Terminate
If you want to return the output of the bat script you will need to read this WScript.Shell.Exec - read output from stdout, and use logic similar to:
Const WshRunning = 0
Const WshFinished = 1
Const WshFailed = 2
strCommand = "C:\Temp\test.bat"
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec(strCommand)
Do While oExec.Status = 0
WScript.Sleep 1000
If Not oExec.StdErr.AtEndOfStream Then
vErrStr = vErrStr & oExec.StdErr.ReadAll
End If
If Not oExec.StdOut.AtEndOfStream Then
vOutStr = vOutStr & oExec.StdOut.ReadAll
End If
Loop
WScript.StdOut.Write(vErrStr)
WScript.Echo(vOutStr)
It all depends on what your bat file is doing really, and the reason you need to kill it after x seconds.
Edit:
Because your batch file is a continuous loop, it may confuse ReadAll of the output stream. You might be best using something such as (note that you will not see real-time output):
Dim strCommand : strCommand = "C:\Temp\test.bat"
Dim WshShell : Set WshShell = CreateObject("WScript.Shell")
'execute command
Dim oExec : Set oExec = WshShell.Exec(strCommand)
'wait 2 seconds
WScript.Sleep 2000
'terminate command
oExec.terminate
'get output
wscript.echo oExec.StdOut.ReadAll
Set oExec = Nothing
Set WshShell = Nothing

How to open webpage in VBS

My code is
Set WshShell = CreateObject("WScript.shell")
for i = 0 to 50
WshShell.SendKeys(chr(175))
next
Process.Start("CMD", "/C start chrome.exe http://www.example.com")
It sets the volume to full, then opens chrome to example.com. But when I run it I get this error:
Cannot use parentheses while calling a Sub
How can I get it to raise the volume, and go to the webpage?
Try like this way :
Option Explicit
Dim URL,WshShell,i
URL = "www.yahoo.com"
Set WshShell = CreateObject("WScript.shell")
For i = 0 to 50
WshShell.SendKeys(chr(175))
Next
WshShell.run "CMD /C start chrome.exe " & URL & "",0,False
wshshell.run "www.youtube.com"
VBScript requires the CALL keyword when calling a sub with parenthesis. You can either write the code like this:
Set WshShell = CreateObject("WScript.shell")
For i = 0 To 50
WshShell.SendKeys Chr(175)
Next
Process.Start "CMD", "/C start chrome.exe http://www.example.com"
...or like this:
Set WshShell = CreateObject("WScript.shell")
For i = 0 To 50
Call WshShell.SendKeys(Chr(175))
Next
Call Process.Start("CMD", "/C start chrome.exe http://www.example.com")
Note: you don't get this error when calling a function and using its return value, like this:
Dim strTest
strTest = SomeFunction()
...because VBScript always requires the parenthesis when a function is used in an assignment.
This way works for me.
set wshshell = wscript.CreateObject("wscript.shell")
wshshell.run "chrome.exe https://stackoverflow.com"
You can change chrome.exe to whatever browser or the website to whatever you want

How to kill specific HTA based on window title

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

Resources