Closing a startup program programmatically in Vista/Windows 7 - windows-7

I am looking to be able to write a script that will close an application that is included in the Windows start up. I know the common sense is why not remove it from start up, but I want it to run because the application connects my NAS. I just close it every single time because I have no use for the actual app except for the fact that it makes the connections.
So I got the bright idea that perhaps I could write a script that will also run in the startup after the app is launch that will kill/close it.
First does this sound possible? Is there a better way?

You can create a .bat file and run it during startup, after the NAS program has run. The following code will kill all instances of a process (the current code will kill notepad)
Dim WMI, KillProc
Dim processName: processName = "notepad.exe"
Set WMI = GetObject("winmgmts:\.\root\cimv2")
Set KillProc = WMI.ExecQuery("Select * from Win32_Process Where Name = '" & processName & "')
For Each Proc In KillProc
Proc.Terminate()
Next

Is the app mapping drives to shared folders on your NAS? If so, then you could remove it from the Startup folder and either:
use Explorer's "Map Network Drive" feature
write a BAT file to run on Startup that does a NET USE to map
write some VBScript :), using WScript.Network.MapNetworkDrive

Related

How to give script a name?

I have a script that I run to keep my computer from going to sleep. It's a simple script that presses the Num Lock key. How can I give my script a name so I can see it in Task Manager? I would like to end the process every now and then and not sure which application it is.
Here is the code (idle.vbs):
Dim objResult
Set objShell = WScript.CreateObject("WScript.Shell")
Do While True
objResult = objShell.sendkeys("{NUMLOCK}{NUMLOCK}")
Wscript.Sleep (60000)
Loop
Your script is being executed in an interpreter, and in Task Manager you will see the name of the executable : wscript.exe
You cannot change the process name, although you can identify the name of the running script using another script and the property of handles.
But the easiest way would be to make a copy of the executable wscript.exe, rename it with something suggestive for you, and use that executable in cmd to run the script. For example idleEx.exe and run it :
...\idleEx.exe idle.vbs
Or, the other method: create a shortcut for the vbs and change Properties / General / Opens with, browse and choose idleEx.exe.
After that, your process name will apear as idleEx.exe

Run batch file in background

I've got a batch file that I want to run in the background whenever the system (Windows 2007) is turned on. The batch file monitors the task list for a given program, and when it sees that it's shut down, prompts the user to de-licence it.
I'm currently trying to do this without converting the batch file into either an executable or Windows service file.
I've found more online references than I can count which tell me that I should use "start /b file.bat" to run the batch file in the background, but this doesn't work for me, it just starts up the batch file in the same cmd line window that I'm using.
Can anyone suggest either what's going wrong, or even better; a nice simple way for me to get the batch file to run ion start up (I cannot use a GUI as I have to roll this out to several computers remotely)
Thanks
You could make a shortcut to your batch file and place the shortcut in your Startup Programs directory:
C:\Users\\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
Since you have to roll this out to several computers remotely, you should be able to copy the batch file to the startup programs directory over the network assuming the remote machines have WinRM enabled and your account has adequate permissions.
If you want this batch file to run in the background at start up, you could reference your batch file from a VBScript (instead of using the batch file's short cut) and set the VBscript to run in invisible mode:
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "C:\path\to\your\batchfile.bat" & Chr(34), 0
Set WshShell = Nothing
Just give this vbscript file the .vbs extension.
If the program you are concerned about is a GUI program (ie non console) just wait for it to exit. Batch waits for GUI programs to exit (but not when started interactively).
notepad
echo My notepad exited
Start /b says to start program in same window. See Start /?. Also Start is usually the wrong command to be using. It starts programs in abnormal ways. See end of http://stackoverflow.com/questions/41030190/command-to-run-a-bat-file/41049135#41049135 for how to start programs.
This is a VBS file.
This monitors for notepad exiting, pops up a messagebox, and restarts notepad.
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 should use less battery power and CPU cycles. Batch files are read line by line so make VERY poor background tasks.
If the program you are concerned about is a GUI program (ie non console) just wait for it to exit. Batch waits for GUI programs to exit (but not when started interactively).
notepad
echo My notepad exited

.ShellExecute and Wait

I've got a VBScript that calls multiple other scripts. (Most of them are just copying files from NAS to multiple places on users PC.
One of the VBScripts needs to be run as administrator using .ShellExecute.
It does work but there are other VBScripts after it's call that rely on it, so I get an error for the files are missing.
I know the command
shell.Run "sample.vbs", , True
for waiting till the script is done.
Is there a similar way to get ShellExecute to wait?
ShellExecute always runs asynchronously. If you have other scripts that need to run only after this script finished you could create some kind of marker upon completion to signal "go" to the other script(s).
For example, you can create a file in a specific path:
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CreateTextFile("C:\path\to\marker.txt", True)
If you need to run your batch of scripts more than once, have the script remove the marker when it starts and re-create it at the end.
The script that's supposed to run next needs to check for this marker and suspend execution until it appears. For example:
Set fso = CreateObject("Scripting.FileSystemObject")
Do Until fso.FileExists("C:\path\to\marker.txt")
WScript.Sleep 100
Loop
Other options are creating a registry value, or an eventlog entry.
Checking the process list (e.g. via Win32_Process) won't work, because normal users don't have access to the CommandLine property of elevated processes (which would be required to identify this particular process).

Command to find Active Drive

I am trying to build a VBScript to automatically run some .exe files. The problem is that the script and the .exe files are on a flashdrive, so it needs to find the current drive letter by itself. I can do it on a batch file using %~d0, but I like some of the functions of VBScript better, especially the ability to send keystrokes. Anyways, I found a whole list of VBScript commands, but I am no expert and I need help with the syntax. So far I have it set to open the task manager and press some keys to have it select the "performance tab" of the task manager:
Dim Act :Set Act = CreateObject("Wscript.Shell")
Act.Run("taskmgr.exe")
Success = Act.AppActivate("taskmgr")
Wscript.Sleep 250
Act.SendKeys "{TAB 5}" :WScript.Sleep 500
Act.SendKeys "{RIGHT 3}" :WScript.Sleep 500
I'd like to know what command I need to use to tell the script to use the drive letter where the script was executed from (USB drive).
Use the .ScriptFullName property to get the full file spec of the running script and apply .GetParentFolderName for the folder's path or .GetDriveName for just the drive letter.
>> Set oFS = CreateObject("Scripting.FileSystemObject")
>> s = WScript.ScriptFullName
>> WScript.Echo oFS.GetParentFolderName(s), oFS.GetDriveName(s)
>>
M:\bin M:
cf. here

Windows cmd: How to find and kill .bat file that was run and hidden with wscript?

I was playing with running a Turntable.fm bot, and I wanted to run it on my local machine's Nodejs installation, but not show the cmd window. So I ended up using a vbs script to hide the window:
CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 0, False
And then my .bat file looks like this:
.\node.exe .\Turntable-API\bots\carlin.js
That's it. Pretty simple. I created a shortcut file in Windows that then ran this:
C:\Windows\System32\wscript.exe "C:\ttbot\invisible.vbs" "C:\ttbot\ttbot.bat"
The issue now is that I have no idea how to find or kill the process that was started. When I try to look in my task manager, there's no wscript.exe, or cmd.exe, or node.exe, or invisible.vbs, or ttbot.bat running process of any sort. When I try running the .bat file in cmd, it doesn't close immediately, so I assume it needs manual shutdown of some sort. What should I do?
Try using Process Explorer, search for any of the processes that you mentioned above incase the processes are running under an existing process, find that, then you can kill yours.
If it doesn't show up in Process Explorer the likelyhood is the script isn't running or it ended after execution, possibly because of an error. Try running without the vbscript to make sure everything is ok.

Resources