How to give script a name? - windows

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

Related

VBS Command "launch.run" no longer executing

We have a vbs script that runs once a month. The script ran well for the most part but as a couple months ago one command stop working
The vbs script is executed by Windows Scheduler
Inside this script we set a variable called "launch"
Set launch = WScript.CreateObject( "WScript.Shell" )
and later in the program we use this command
launch.run("runthis.BAT")
We do not specify the full path in the command and to my knowledge we never have. This bat file resides in the same directory that the vbs script does. I guess this is why it was setup to run that way.
We are not sure what happened and the sys admins are not sure what could have changed to cause it to no longer work.
Please keep in mind we are not VBS specialists and this is something that has been in place for several years.
Any suggestions or resources to look at would be appreciated.
In order for us to provide a better answer, please update your question with the script you are having trouble with. We don't know what the launch object is - hopefully something other than WScript.Shell. Nevertheless you should be able to run a batch file this way:
Dim objShell
Set objShell = CreateObject("WScript.Shell")
objShell.Run "C:\your_folder\your_batch_file.bat"
Replace the launch.run part of your script with this code and update it with the correct path and name of your batch file (update "C:\your_folder\your_batch_file.bat" part).

Wscript make proccess not run on background or wait while finishes

Scenario: We have some scheduled jobs (Control-M) running many proccesses and some of them execute a .VBS file (with parameters) that reads the configuration from an XML file and sends it to a company's software that interprets it and loads data onto tables.
I need help with the VBS file, that as stated above, only gets the instructions from the XML and send it to the software with these steps (also, logs each step):
Finds the XML;
Creates an object to login the software (with XML parameters);
Dim object
Set object = CreateObject("service.location.id")
Login into the Database (with XML parameters);
object.Login("DATABASE_NAME")
Select which Data base (XML...);
object.SelectDatabase("DATABASE_NAME")
Sends command to start load proccess
object.LoadRepositoryTable(XML)
The problem is that since the default interpreter is wscript, when executing the script, it runs on the background and the Job Scheduler thinks it finished executing and starts the next job.
Executing the script on CMD, if I start it as cscript SCRIPT.vbs it waits for the whole load proccess to finish (it doesn't run on background), and I want to do this when running as wscript - because since there are many jobs, editing how they calls the script is not an option right now. This includes creating an .bat file that calls the SCRIPT.vbs as cscript.
I also tried searching on "how to run another .vbs file inside a VB Script" to run as cscript, but didn't manage to make it work.
What can I do?
Is there a way to make wscript wait for the load?
Is there a way to call a .vbs in the same folder (without sending it as parameter) while passing 2 arguments?
Another option I'm not seeing?
Edit: Here is what I've come to so far, but I'm getting "expected end of statement".
Dim objShell
Dim arg_dir
arg_dir = Wscript.Arguments.Item(0)
Dim arg_xml
arg_xml = Wscript.Arguments.Item(1)
Set objShell = CreateObject("Wscript.Shell")
objShell.Run "cmd /k cscript ""C:\Folder\Scripts\SCRIPT.vbs" &" "& arg_dir &" "& arg_xml"", 1, True
cscript is the solution for your problem. Either have your scheduler run each VBScript with cscript.exe, or (if that isn't possible for some reason) make cscript the default script host by running
wscript.exe //h:cscript
once as an administrator.
The script you have cobbled together won't resolve your problem, even if you fixed the syntax error in the last line, because as long as you run it with wscript.exe it will still return immediately. It doesn't matter that the code in that script is running other scripts synchronously as long as all of that is happening in the background.

On boot, issues running a bat file through vbs script

I need to run a program (a python script made into an exe) on start up, without the console showing up.
In some question, I found the solution, i.e to execute the program. Right now, I'm testing it out with a simple python program filewriter.py that does -
while count != 1000:
f = open('test.txt','a+')
f.write(str(count))
f.close()
sleep 1
The bat file tool.bat :
#ECHO OFF
python "<absolute_path_here>\filewriter.py"
EXIT /B
The VBS file :
Set WinScriptHost = CreateObject("WScript.Shell")
WinScriptHost.Run Chr(34) & "<absolute_path_here>\tool.bat" & Chr(34), 0
Set WinScriptHost = Nothing
If I execute the VBS file (double-click it), everything works fine. The output file appears, without the console appearing. So I added it to registry under
HKCU\Software\Microsoft\CurrenVersion\Run
as WScript "path_to_the_vbs_file".
On Startup, the VBS file executes properly (verified it by adding a MsgBox which displayed the popup) but the call to the bat file is not being executed. How do I make this work?
In windows there are two python executables: python.exe, pythonw.exe. If you don't wish to see the terminal window you must use pythonw.exe.
I need to run a program (a python script made into an exe).
If you covert your script to .exe with help of py2exe it is simillar. You can assing your script to console or windows. Look to Py2exe Tutorial, the console variable can be replace forwindows.
You don't need to create EXE files from python. You can run pythonw.exe with path as argument to your script. Why do you need to create .bat which you run from vbscript ? Look here: Run on windows startup CMD with arguments
I forgot to say, that the Windows Python installer normally create following file association, so the script can run directly.
'.py' to python.exe
'.pyw' to pythonw.exe
Other way how you can run the the script on boot is to use Windows Scheduler. The big advantage is you can setup user rights or more events when start the script. You can run the script manually too and you will last status.
Create Python .exe is sometimes tricky. If you don't need to distribute your script to multiple computers I prefer don't use.

.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).

How do I make a shortcut for a Perl program under Windows using a batch file?

I'm trying to "hide" some of my Perl program from the end user to make things easier on them. I'm doing what I can to keep them out of the command prompt. The program itself has a GUI designed in Perl/Tk, so they don't have to worry about the command prompt.
Could I write out a quick batch file that goes along the lines of:
START perl 'C:\[some path here]\myscript.pl'
with START to start a program, the Perl interpretor as my program, and the path/name of my Perl script as the parameter?
Would I have to specify where to find perl or would Windows just know because perl is in the computer's PATH variable?
I have a totally evil VBS script and batch file to deal with this kind of thing with Strawberry Perl Portable. The VBS script runs the batch file without spawning a cmd window. The batch file works out where it is, where perl.exe is relative to it, and runs the perl script. In this case it's to run a Catalyst server, but can be used for anything else too.
Here's the vbs (shudder):
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & fso.GetParentFolderName(wscript.ScriptFullName) & "\perlshell.bat"& Chr(34), 0
Set WshShell = Nothing
And here's the batch script (only slightly less shudder):
echo off
set bindir=%~dp0
set perlpath=%bindir%perl\bin
set buildpath=%bindir%\bin
set PATH=%PATH%;%perlpath%;%buildpath%
"%perlpath%\wperl.exe" "%bindir%MyPhp\script\myphp_server.pl" -p 35900
you don't need "start" for this. either add perl.exe from your path or specify the full path to the perl interpreter.
so ... put this in your batch file:
c:\program files\perl.exe "c:\whatever\myscript.perl"
-don
If the idea is to keep them away from the command line, why use a batch file at all? Why not just make a shortcut? (You could use Win32::Shortcut if you don't want to make one by hand.)
There's an easier way. For windows:
Go to the desktop -> right click -> create a shortcut
make your target look like (i.e. the path to the perl.exe file) something like the following - I'll show you what mine looks like
C:\Strawberry\perl\bin\perl.exe
In my case I have Strawberry Perl installed
right click desktop shortcut -> click properties
In the target input box, alter it to look something like mine:
C:\Strawberry\perl\bin\perl.exe D:\Developer\CustomTools\login.pl
where login.pl is the absolute path to my perl script
from there you should have a shortcut created to allow execution of a perl script.
In case you might be interested:
in the properties popup you can also assign this invocation of the perl script to a keyboard button press and assign it a shortcut key to launch anytime ^_^
For me, I chose to do:
ctrl+alt+i
to stand for login - to invoke the login.pl script I wrote. be aware of default system keyboard combinations
If you have any questions, let me know!
Thanks for you time!

Resources