Tuesday, April 21, 2009

Batch or VBScript cannot run as scheduled task on Windows 2008/Vista, why?

Have you ever experienced the following problem?

The script file runs fine as a scheduled task under Windows 2003/XP but it fails on Windows 2008/Vista. If you run the script manually on the command prompt on Windows 2008/Vista, it will work perfectly without a problem. It always fails when it runs as a scheduled task regardless of logon. Task Start Failed always occurs with the default error code 2147750687.

The problem spent me almost 2 months to figure out while I was busy with something else. It happened almost 8 months ago from now when everything was being moved from Windows 2003 to Windows 2008.

The followings were the scheduled task settings on Windows 2008/Vista:

  • Run under the local Administrator account
  • Run whether user is logged on or not
  • Run with highest privileges - actually this one shouldn't be needed since it is run under the local Administrator.

Task Scheduler 2.0 took place on Windows 2008 as well as Windows Vista. There are a lot of differences from its predecessor on Windows 2003 or Windows XP. Here is one of them. When you run your script as a scheduled task, the scheduler (2.0) will create an account impersonating as the user you configure/specify for the task. The account is always resided in or runs from %systemroot%\system32\. Therefore, if your script internally uses relative path, it is going to fail 100% because of path/file not found or permission denied. For that reason, there are two options I would suggest:

  • Modify all the relative paths to be fully qualified paths or absolute paths; e.g., c:\tasks\scheduled\output.txt
  • Modify the script to take the directory as an argument when you run it; e.g., c:\tasks\scheduled\taskA.vbs c:\tasks\myappPath\.

In my case, as soon as I've fixed the relative path problems, all scripts with Task Scheduler 2.0 run like a charm.

In sum, checking if the problem is related to a relative path on your mystery task may save you from a million troubles.

1 comment:

  1. Thanks !!! Took me a while to figure out that I needed to use the static path when running under 'runas' ...