Scheduling VB Scripts via Scheduled Tasks

Written by : Marcos Velez 

imageEver find yourself having problems when trying to schedule a VBScript (that requires some command-line parameters) using Windows Scheduled Tasks? If so, read on.

Normally, when scheduling a task, you simply specify the path of the application being launched, followed by the intended schedule and the account under which the execution is taking place. If you need to specify any command-line parameters, you can do so by modifying the properties of the scheduled task and adding them to the Run field at the end of the specified application’s path. Alas, when it comes to VB scripts, it is not so simple. While you can certainly schedule a script to execute at any given interval by specifying its path in the Run field, you can not pass to it any parameters by simply specifying them after the script’s path.

For example, suppose you have a script, called recordEvents.vbs, that crawls through the Event Logs and records the information contained in there. Assuming the script is located in the root of the C:\ drive, you can specify its path (C:\recordEvents.vbs) in the Run field and all will be well. But, if you need to specify the type of event to record, by passing a command-line parameter such as "/ERRORONLY", you can not use "C:\recordEvents.vbs /ERRORONLY" as your Run path. It simply will not work. Mind you, you will not see any errors, but recordEvents.vbs will be executed without the specified parameters. Even if you encapsulate the entire path in quotation marks, it will not work. Encapsulating the whole thing in quotation marks tells the OS to execute a file called "recordEvents.vbs /ERRORONLY". Obviously, that doesn’t work.

So, what is the solution? When using Scheduled Tasks to schedule a VB script that requires command line parameters, you must launch your script using wscript.exe (windows-based script host) or cscript.exe (command-line-based script host). For example,

wscript.exe C:\recordEvents.vbs /ERRORONLY
Or,
cscript.exe C:\recordEvents.vbs /ERRORONLY

For more information on using cscript.exe, refer to https://technet.microsoft.com/en-us/library/bb490816.aspx.
For more information on the differences between wscipt.exe and cscript.exe, refer to https://www.microsoft.com/technet/scriptcenter/guide/sas_wsh_divn.mspx?mfr=true. Lastly, for more information on options that apply to both wscript.exe and cscript.exe, refer to https://msdn.microsoft.com/en-us/library/xazzc41b(VS.85).aspx.

TAGS