3

I'm trying to run a .vbs file as a scheduled task through Windows Task Scheduler. Under the 'General' tab, when I select "Run only when user is logged on", the script executes as expected.

However, when I select "Run whether user is logged on or not", and enter the appropriate credentials, the task runs at the scheduled time, but the script does not actually run. I've already tried running the script under wscript.exe as well as cscript.exe, but no luck with either.

EDIT: Even if I am logged in when the task begins, the script will still not run under the "logged in or out" setting.

Additional info: The purpose of this scheduled task is to run before I arrive at work. I've already configured my BIOS to startup at a predetermined time (06:00), and set the Task Scheduler to run at 06:27. I've successfully tested the BIOS startup, as well as the script itself (including using the Task Scheduler to run it). Therefore, the only weak link I can find is the option to "Run whether the user is logged on or not".

I'm running Windows 7 Enterprise.

Any help would be appreciated!

Arne
  • 155
  • 1
  • 1
  • 7
  • Not sure its possible to do this. Both WScript & CScript require output which is considered "desktop interaction". Some more details (a literal console app, instead of WScript/CScript, but the same rules apply) http://stackoverflow.com/questions/1369236/how-to-run-console-application-from-windows-service – Marvin Smit Sep 08 '14 at 21:06
  • What you're saying makes sense, however shouldn't the script then execute properly when I am logged in (see edit)? That being said, would scheduling a task to LOG IN first before attempting to run the script solve this problem? (I will test this shortly). – Arne Sep 08 '14 at 21:18
  • Okay I've done some more research: http://social.technet.microsoft.com/Forums/windows/en-US/c03d6691-b058-4f8d-961c-e8eba25bbaed/task-scheduler-problem-run-whether-user-is-logged-on-or-not?forum=w7itprogeneral I think that the "desktop interaction" issue may be the cause of this problem. – Arne Sep 08 '14 at 21:25
  • AFAIK; an aborted script doesn't report anything. Hence you can see the start of the process, but it abort on "no input/output channel" is my guess. silently failing the execution of the script. – Marvin Smit Sep 08 '14 at 21:34

1 Answers1

4

This is because normally it would run the script using the shell handler, which by default is wscript.exe. When there's no desktop environment (because no-one is logged-in) it would fail and abort script execution (or rather, not run the script in the first place).

To fix this, instead of running the .vbs file directly, change it to run cscript.exe (the command-line script runtime program) with the script's filename passed as the first argument. Also be sure to ensure you don't have any InputBox or MessageBox calls (instead use WScript.Echo to return messages to the user: wscript displays message-boxes, but cscript will write it to the console.

Dai
  • 141,631
  • 28
  • 261
  • 374
  • I've already tried running `cscript.exe` with the script's filename as the argument, but the task has simply "completed" without running the script. I also checked the script, and there are no calls to `MessageBox` or `InputBox`. – Arne Sep 08 '14 at 21:12
  • @Arne what is the "Last Run Result" of the task? What is the exact command you're running? Did you enclose the script filename in quotes? – Dai Sep 09 '14 at 04:01
  • I've played around with a few variations on the command, but I'm currently using `cscript.exe` in the "Start Program/Script" box, and passing a couple options and the script filename as the optional argument (in quotations): `//nologo //B "C:\folder\myscripthere.vbs"` – Arne Sep 09 '14 at 22:58