I'm doing a mash between VbScript and CMD, i can call the VBScript easily with
cscript.exe //NoLogo "%~dp0TASK.vbs" >>"%~dp0output.txt"
But I need to disable the feature of users clicking on the VBScript and calling all sorts of errors, rather than it being called through a batch file.
My first attempt was a mess of setting a variable into a text file before i ran cscript.exe
and use error handling in VBScript to tell if that variable could be collected, but it added too much time to the script.
Does VBScript have a way to tell whether it was started by CMD, or simply by double clicking, and able to act accordingly?
Here is a simple function, detecting the parent process caption. You can check if the process was started by CMD shell (the caption is
cmd.exe
) or by double-click (explorer.exe
):In the context of your question another method allowing to pass parameters from CMD shell process to WSH script child process may be useful. It uses environment variable and
WScript.Shell
object. Consider the below example.There is code for
task.cmd
file:And for
task.vbs
file:I have got the output as follows:
Note, process environment variables are accessible for child processes only.
One way is for your VBS file to check for the presence of parameters and if they do not exist then stop the execution.
In your VBS script:
When you call your VBS file, just passing any parameter will satisfy the condition:
This will not stop people from running it manually (with a parameter) or creating a shortcut which has a parameter. It would only really stop running the VBS directly (as a parameter will not be passed).
If the bat file will keep the cmd.exe open while the vbs file runs, you can try to detect the cmd process inside the vbs file to continue execution.
Put this at the start of your vbs file:
When you double click on a
.vbs
file, the action is determined by the following registry key:If you were to change the key, you will be changing the double click action, but you will not be affecting your ability to launch the command explicitly via invoking
cscript.exe
directly.