VBS scheduled task cannot run under Windows 2008

By mclai ยท 7 replies
Nov 22, 2008
  1. The script is written in VBS. Since it is moved over to Windows 2008, it has never run by the task scheduler. "Task Start Failed" always occur with the default error code 2147750687 regardless of logon. The script runs well under the command prompt.

    The following is the task setting:
    - Run under the local Administrator account
    - Run whether user is logged on or not
    - Run with highest privileges

    I have been researching this problem for 2+ weeks and I am still unable to resolve it. Then I wrote a very simple script to see where it fails. The script only contains the following 4 lines.

    Set oScript = CreateObject("WSCRIPT.SHELL")
    Set oFileSys = CreateObject("Scripting.FileSystemObject")
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objTextFile = objFSO.CreateTextFile("ftp\tmp.txt")
    It looks like that the script fails when it is trying to call CreateTextFile(). If I comment the last line, the task for this script can be completed without an error.

    I also tried to specifically add "Administrator" into Local Security Policy for "Log on as a batch job" (see Local Security Policy -> Local Policies -> User Rights Assignment -> Log on as a batch job). It is no help to this problem.

    I tried again with my XP box. Everything is fine.

    I guess the problem could be related to the User Security Context under task scheduler 2.0. Unfortunately, I am still unsure where or how I can make the task to run or access the resource.

    Is there anyone who has an idea how to fix this? As I mentioned, the task is run under local administrator and has elevated with the highest privileges. The script runs fine in a command prompt but not a scheduled task. Do I miss some important settings? How can I run a task that is written in VBS and needs to access (read/write) into the resource under Windows 2008 server? Any pointer is appreciated. Thanks.
  2. kimsland

    kimsland Ex-TechSpotter Posts: 14,523

    What happens when you do this command:

    Set objOutputFile = objFSO.CreateTextFile("output.txt")

    output.txt = some place like c:\output.txt

    Also see I changed the set objOutputFile
  3. mclai

    mclai TS Rookie Topic Starter

    Thanks. It works.

    Yes, it also works with the fully qualify path name as you described, e.g., c:\output.txt, c:\ftp\output.txt.

    May I know what is the relative path that the script is running from by the scheduler?

    If I use, Set objOutputFile = objFSO.CreateTextFile("output.txt") , there is no error for the scheduled task but I don't see and find the output.txt in the system. Where/which folder will it be located/created?
  4. kimsland

    kimsland Ex-TechSpotter Posts: 14,523

    That's strange if you replace output.txt with c:\output.txt, shouldn't you then find output.txt in C drive root? ie c:\output.txt ?
  5. mclai

    mclai TS Rookie Topic Starter

    Yes, if I use absolute path: c:\output.txt, I can see the file at c:\ directory.

    But if I use output.txt, the script (run by scheduler) has no error but I cannot find the file. I just wonder where the file is stored for output.txt.

    I don't understand why the relative path is not working with task scheduler 2.0.
  6. jobeard

    jobeard TS Ambassador Posts: 11,173   +989

    ftp\output.txt should be in the context of the %USERPROFILE%. the directory ftp must pre-exist
  7. kimsland

    kimsland Ex-TechSpotter Posts: 14,523

    No where
    output.txt is not the location (that's why I didn't bold it above)
    You must replace output.txt with you absolute (or enviromental) location
    ie %SystemDrive%\ftp\output.txt
    As long as "ftp" folder exists of course
    This one: %USERDOMAIN% if you substitute Userdomain with any user domain you might have can possibly help too.

    output.txt is just the example. I should have said DriveLabel:\folderName\output.txt
  8. mclai

    mclai TS Rookie Topic Starter

    No, I don't find the output file at %USERPROFILE%. Finally, I found it at %systemroot%\system32\. It implies that the task scheduler creates a special profile in %systemroot%\system32\ and then loads the program in that profile to run. That special profile is something different from the original account on which the task is created.

    Because of this, using the application or current path (see below) to compose/retrieve the location will not give me the correct location of the program either and the script will fail when it runs under the scheduler.
    curPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".") & "\"​

    When the program runs under the scheduler, curPath is always pointing to %systemroot%\system32\.

    At this moment, I cannot find any better solution to resolve this, other than hard-coding the location into the script.
Topic Status:
Not open for further replies.

Similar Topics

Add your comment to this article

You need to be a member to leave a comment. Join thousands of tech enthusiasts and participate.
TechSpot Account You may also...