How to end a file using vbs without starting anything? - vbscript

So, I'm trying to make a security system using vbs that would terminate a process.
Ex:
If you see "cmd.exe"
Terminate cmd.exe
I have coded everything except for the terminating part. I have searched many places but all they do is first run that file and then terminate it. I want to terminate the existing running "cmd.exe"
I tried running a batch file that terminates all "cmd.exe" processes but it just terminated itself leaving the other "cmd.exe" open.
Powershell is not an option too because it's one of the process I want to terminate.
Regards,
A Viper

Running TaskKill Command with VB Script
TaskKill command can terminate all existing processes whose running in same image name.
For example, if the process you want to be terminated is "cmd.exe", you can use TaskKill as below in your script:
Dim WshShell : Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "CMD /C TASKKILL /F /IM cmd.exe", 0, False
Above script is executed using Windows Script Host, which is not a process you want to be terminated, so this should work.

Related

Windows batch file - how to execute command after program close that's not started in the batch file

I'm having a hard time finding an answer to this question. I'm looking specifically to execute a command in a batch file only after a program is terminated - however, a program that wasn't launched by the batch file.
My problem is this - the program I am actually launching in the batch file I want to wait on is in turn launching another program, which in turn launches another. This probably doesn't make any sense - but it's because it's a game launcher. It is for Final Fantasy XIV. The normal program that is launched to start it is ffxivboot.exe, which in turn launches ffxivlauncher.exe. That is a login window, and once you login, it in turn launches ffxiv_dx11.exe. So while I originally wrote it to wait on ffxivboot.exe, that process doesn't stay running so I am unable to wait on it.
Here's my file (excluded paths for simplicity):
taskkill /im someprogram.exe
ffxivboot.exe
timeout /t 60 /nobreak
### ??? need to wait on ffxiv_dx11.exe to close before executing next command
someprogram.exe
I added a timer to wait so that it gives me plenty of time to login - because the ffxiv_dx11.exe process doesn't start until after logging in.
Is what I'm trying to do possible? It's hard to search for answers to this because I only get results regarding when you're waiting on a task to end that was started from the batch file. But like I said, that one launches another which in turn launches another - so the original process is no longer running.
Thanks for any help!
taskkill /im someprogram.exe
ffxivboot.exe
timeout /t 60 /nobreak
:repeat
::### ??? need to wait on ffxiv_dx11.exe to close before executing next command
tasklist /fi "imagename eq ffxiv_dx11.exe"|find /i "=========================" >nul 2>nul &&(
w32tm /stripchart /computer:localhost /period:10 /dataonly /samples:2 1>nul
goto :repeat
)
someprogram.exe
try this
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")
Set objEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM Win32_ProcessStopTrace")
Do
Set objReceivedEvent = objEvents.NextEvent
msgbox objReceivedEvent.ProcessName
If lcase(objReceivedEvent.ProcessName) = lcase("Notepad.exe") then
Msgbox "Process exited with exit code " & objReceivedEvent.ExitStatus
WshShell.Run "c:\Windows\notepad.exe", 1, false
End If
Loop
This is a vbs file that needs to run elevated. It waits for any program to exit and checks if it's notepad and springs to life restarting it.
Type in a elevated command prompt taskkill /f /I'm wscript.exe to stop it.

Simple shutdown script does not function properly after PC wipe

I had a simple VB Script that would let me enter in the amount of minutes before I wanted my PC to turn off by itself, and then it would auto-shutdown. That worked fine. After I wiped my PC, the script no longer functions as intended, instead showing a blank cmd window after I enter the number of minutes before shutdown, and displays the inputbox again (asking for # of minutes before shutdown).
Any ideas on why this won't function correctly, and why it worked before but not now? Do I need a certain package from Microsoft that maybe I didn't reinstall?
Code:
Dim a
Dim oShell
a=inputbox("After how many minutes would you like to shut down your PC? Enter cancel to cancel a previous shutdown")
Set oShell = WScript.CreateObject ("WScript.Shell")
if a = "cancel" then
oShell.run "cmd.exe /c shutdown /a"
elseif a = "" then
MsgBox"Please enter after how many minutes you would like to turn off this PC",0+16,"Enter a number"
elseif a = "0" then
b=msgbox("Are you sure you want to shut down this PC immediately?",4+32,"Shut down immediately?")
if b = "6" then
oShell.run "cmd.exe /c shutdown /s /f"
end if
else
oShell.run "cmd.exe /c shutdown /s /t " & (a * 60)
end if
EDIT: Running the script from its directory works as intended, but running the VBScript from a shortcut (as a I had been doing) doesn't work and yields the above results.
EDIT: Also the script itself won't run properly on my desktop, but runs fine in the folder I store my scripts.
You named the script shutdown.vbs and run it with the working directory set to the directory containing the script. By running oShell.Run "cmd.exe /c shutdown ..." your script is effectively calling itself.
If you call a command shutdown (without path and extension) the system is looking for a file with one of the extensions listed in %PATHEXT% in the directories listed in the %PATH% environment variable. The first match wins.
Since on Windows the current directory comes first in the %PATH% the file %CD%\shutdown.vbs is found before %windir%\system32\shutdown.exe.
Either rename your VBScript or change cmd.exe /c shutdown to cmd.exe /c shutdown.exe and the problem will disappear.

Open windows batch with prompt text but no invokation

How can one open a batch window with some predefined text after the prompt and not invoking the command?
Say I want to invoke notepad.exe with a filename t.txt.
I would create a cmd file with this line:
start notepad "t.txt"
But I want the file to be opened specified by the user.
So the cmd file should just open a cmd window and "type" start notepad without actually executing this.
You can do it using a Vbscript:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "cmd", 9 'opens cmd.exe
WScript.Sleep 500 'gives cmd a time to load
WshShell.SendKeys "start notepad"
If you want this within your cmd file, try this:
#if (#X)==(#Y) #end /*
start cmd.exe
cscript //E:JScript //nologo "%~f0"
exit/b
*/
var obj = new ActiveXObject("WScript.Shell");
obj.SendKeys("start notepad");
Normally you'd have to launch a command prompt window: start cmd rather than starting notepad directly. But neither start nor cmd offer the function you desire. Would adding a simple echo to your batch file with instructions and a pause prior to actually issuing the command work?
Echo When the other operation completes return to this window
Pause
start notepad "t.txt"
This will provide the echo, then wait for the user to press any key before actually attempting to launch notepad with that filename.

Terminate process SILENTLY using VBScript

I am simply trying to kill a process using a batch that calls a vbscript, but i need the vbscript to do it silently. Or can this not be done without UAC prompting for privileges?
Used WMIC instead
wmic process where name="wscript.exe" call terminate > nul
You can TASKKILL a process with PID on a remote system, using different user credentials, and TASKLIST to list them.
At a command prompt type taskkill /? or this taskkill on technet. Using PID will prevent terminating a wrong process with the same. For example, a computer with both 2007 and 2010 versions of office installed and only the 2007 version needs terminating.
See tasklist /? or this tasklist on technet.
You can even combine PSEXEC to fill the gap which TASKLIST cannot do remotely.
To run a script or application in the Windows Shell using the ShellExecute method (cf msdn.microsoft.com):
Syntax
.ShellExecute "application", "parameters", "dir", "verb", windowFlag
Key
application The file to execute (required)
parameters Arguments for the executable
dir Working directory
verb The operation to execute (runas/open/edit/print)
windowFlag View mode application window (normal=1, hide=0, ...)
You can try this:
Set denyUAC = CreateObject("Shell.Application")
denyUAC.ShellExecute "cscript", "D:\demo\vbscript.vbs", "", "runas", 0

How to close the parent cmd console window from within the script it's hosting?

I am using the following command in the registry to run a vbscript locally from Windows Explorer's context menu:
cmd /T:1F /K "cscript.exe //nologo "C:\Some Path\Some Folder\MyScript.vbs" "%1""
I am using the /K switch for 2 reasons: to keep the console window open in case of script errors, and to change the back and fore colors of the console (this is actually done with the /T switch, but for some reason that I don't understand, the colors revert to default when the /C switch is used.
My question: how can I close the host console window (attached to the "cmd.exe" process) on normal execution end? WScript.Quit obviously returns to the console prompt. I thought about a WMI query for Win32_Process based on CommandLine, but that doesn't work if the script is invoked multiple times with the same parameter (i.e. file). Is there a way to obtain the top most parent process's id for example, which I can then use to terminate?
I believe you can simply append &&exit to your command string:
cmd /T:1F /K "cscript.exe //nologo "C:\Some Path\Some Folder\MyScript.vbs" "%1"&&exit"
Note that your quoted arguments are not quoted because of the extra enclosing quotes. You might be better off with:
cmd /T:1F /K ^"cscript.exe //nologo "C:\Some Path\Some Folder\MyScript.vbs" "%~1"^&&exit^"
Update
The script engine does not set the exit code upon runtime errors - it leaves that task to the script writer. It does set a positive exit code if there is a syntax error. That is a very unfortunate design.
I think the easiest thing to do is to modify your VBS script to always exit with a negative value upon success: WScript.Quit -1. Then you can conditionally EXIT the console only if the exit code is negative.
cmd /T:1F /K ^"cscript.exe //nologo "C:\Some Path\Some Folder\MyScript.vbs" "%~1"^||if not errorlevel 0 exit^"

Resources