Task scheduler won't execute vbs

Discussion in 'Windows Server' started by wpher56, Mar 18, 2009.

  1. wpher56

    wpher56 Guest

    (Windows server 2008 x64) : I have a very basic vbs that I would run every
    hour: it checks the size of a file and stores (appends) the result in a
    logfile. I double-click this vbs and all is fine. Now I create a task in the
    task Scheduler to run it every hour and it won't run. Both the vbs and the
    log are located in the c:\ directory. The history shows
    - task triggered
    - Action started (operational code = 1)
    - task engine received message to start task (operational code = 1)
    - task started (operational code = 1)
    - Created task process

    and that's all. Last Run Result: 0x41301.
    Then I removed the lines concerning the output and I get the same result.
    Looks like the Task Scheduler won't execute vbs files !

    Or did I miss something ?
    wpher56, Mar 18, 2009
    1. Advertisements

  2. wpher56

    Masterplan Guest


    Just put in task scheduler the following command: cscript yourscript.vbs
    Masterplan, Mar 18, 2009
    1. Advertisements

  3. Yes, the Task Scheduler WILL execute .vbs files but you need to give
    yourself some eyes to see what the problem is. Instead of invoking the .vbs
    file directly, invoke it through a batch file like so:
    @echo off
    echo %date% %time% %username% >> c:\test.txt
    cscript //nologo c:\pher.vbs 1>>c:\test.txt 2>>&1
    echo %date% %time% >> c:\test.txt

    Now run this file via the Task Scheduler, then examine the file c:\test.txt.
    I expect that all will become very clear. Note that you could replace
    cscript.exe with wscript.exe, depending on which interpreter you wish to
    Pegasus [MVP], Mar 18, 2009
  4. wpher56

    wpher56 Guest

    Thank you very much for this trick.
    Well, the problem is that I get a "Permission denied" on the line
    set lf=FSO.OpenTextFile("c:\outfile.log",8)
    when I run the command file (not through the Task Scheduler), but NOT when I
    run the vbs directly.
    The user is "administrator", the outfile.log is set to "everyone full
    wpher56, Mar 18, 2009
  5. wpher56

    wpher56 Guest

    Finally I could run the command file after having moved the cmd, vbs and log
    file to the Administrator documents directory !
    But from the Task Scheduler I get "Task Start Failed, Launch Failure"
    Additional Data: Error Value: 2147750687, which corresponds to 0x8004131F.
    This means SCHED_E_ALREADY_RUNNING. So I set the settings to "Stop the
    existing instance". And finally the Task Scheduler shows "Action completed".
    But the job is NOT done: the output of the command file is written to the
    log file, but NOT the output of the vbs.
    wpher56, Mar 18, 2009
  6. Let's have a look at the contents of the log file c:\test.txt!
    Pegasus [MVP], Mar 18, 2009
  7. wpher56

    wpher56 Guest

    Through the task scheduler:

    19/03/2009 7:30:00.19 administrator
    19/03/2009 7:30:00.21 administrator

    Command file directly executed:

    19/03/2009 7:32:00.24 administrator
    File size: 22998765
    19/03/2009 7:32:00.28 administrator

    The line "File size" is output from the vbs.

    wpher56, Mar 19, 2009
  8. The log file shows that your .vbs file did execute, even though the Subject
    of your thread suggests that it didn't. We can therefore consider this issue
    to be resolved.

    You now say that the .vbs program does not generate the output you expect.
    This is an issue within your .vbs code. To resolve it you must post the
    code. Maybe you have a permissions problem that you're hiding by having a
    global "on error resume next" statement in your code. You must not use such
    statements while testing your code unless you wish to make things difficult
    for yourself!

    Alternatively you could use the Task Scheduler to invoke a very simple .vbs
    program, e.g. one that uses the File System Object to write a line of text
    to a file of your choice. This test would be a clear demonstration that .vbs
    code will execute properly, even when invoked by the Task Scheduler.
    Pegasus [MVP], Mar 19, 2009
  9. wpher56

    wpher56 Guest

    :) You are right, the vbs is executed !
    But it does not produce any result.
    This is the code of the testwrite.vbs:

    set FSO = CreateObject("Scripting.FileSystemObject")
    Set lf =
    FSO.OpenTextFile("C:\Users\administrator.UNIFR\Documents\testwrite.txt", 8,
    msg = date & " " & time
    Set lf=Nothing
    Set FSO=Nothing

    The file testwrite.txt exist, the vbs runs fine when double-clicked.

    wpher56, Mar 19, 2009
  10. When debugging programs then you should keep them as simple as possible.
    Instead of writing data to a folder that may be subject to access
    restrictions, select a folder that is accessible to everyone, e.g. like so:

    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set lf = FSO.OpenTextFile("C:\testwrite.txt", 8, True)
    msg = Date & " " & Time

    When this works (as it will!) then you can build your code up until you find
    the cause of the problem. Remember to pay attention to the account you use
    for your scheduled task!
    Pegasus [MVP], Mar 19, 2009
  11. wpher56

    wpher56 Guest

    The only place where the program runs is in the Administrator documents
    directory. Setting it in C:\ causes the Task Scheduler to hang at the step
    "Created task Process". That explains the title of my question "Task
    Scheduler won't execute vbs".
    I tried to put the vbs and the output file in the system32 directory which
    is the home of the task scheduler, with no more success.
    I also displayed the Err.Number value after the FSO.OpenTextFile statement,
    but the value is 0, No error.
    wpher56, Mar 19, 2009
  12. Let's go about this a little more systematically:
    1. Create a folder c:\Test.
    2. Set the permissions of c:\Test to "Full read-write access for everyone".
    3. Copy your .vbs file into c:\Test.
    4. Use runas.exe to start a Command Prompt under the **same account** as the
    one used for the scheduled task.
    5. Put your test VB Script code into c:\test\test.vbs.
    6. Create a batch file c:\test\wpher.bat as I suggested in my first reply.
    7. Execute c:\test\wpher56.bat from the "runas" Command Prompt. Does it run
    as expected?
    8. Execute c:\test\wpher56.bat as a scheduled task. Does it run as expected?
    Pegasus [MVP], Mar 19, 2009
  13. wpher56

    wpher56 Guest

    The problem occurs at step 7, so I did not even tried step 8:

    VBScript runtime error: Permission denied
    Line: set lf=FSO.OpenTextFile("c:\test\testwrite.txt", 8, True)

    The DOS box shows the correct user, the one I use in the Task Scheduler
    (System Administrator). The files and directory c:\test have full access to
    everyone as well as administrators.

    wpher56, Mar 20, 2009
  14. In my previous reply I went to the trouble of spelling out in great detail
    what steps you should take in order to home in on the cause of your problem.
    Your reply is unfortunatly so brief that it tells me next to nothing. I
    don't know what script you're running (appart from a single line of code)
    and I do not know under what conditions you ran the script, e.g.
    - running the batch file directly (what does the batch file look like?)
    - running the batch file via the Task Scheduler
    - running the script file directly
    - running the script file via the Task Scheduler.
    I recommend you ask a person with a strong background in Windows, VB Scripts
    and batch files to assist you on site so that he/she can see things directly
    instead of having to rely on incomplete reports. If this is not feasible and
    if you're desperate to resolve this issue then you can make your machine
    visible to me so that I can see with my own eyes what's going on. It's not a
    solution I recommend but if you want to know more about it then you can send
    a note to pegasus_fnlATyahooDOTcom to find out more.
    Pegasus [MVP], Mar 20, 2009
  15. wpher56

    wpher56 Guest

    I followed the steps 1 thru 7 of your previous mail, so I did not repeat
    Steps 1 to 6: ok, step 7: VBScript Runtime Error, Permission denied.

    The script is what you wrote 2 days ago, except for the path (c:\test
    instead of c:\).
    The vbs is what I posted yesterday, very basic:

    set FSO = CreateObject("Scripting.FileSystemObject")
    Set lf =
    FSO.OpenTextFile("C:\Users\administrator.UNIFR\Documents\testwrite.txt", 8,
    msg = date & " " & time

    I wrote tons of programs in cmd, vb, vbs, fortran, php etc in the past 30
    years, so I believe I have a strong backgroud in that matter.
    I'm System Administrator (Windows Server 2003/2008 and Linux) and DBA (SQL
    Server + Oracle) in charge of about 50 servers in a university with 16'000
    accounts, I suppose I have also enough background in that matter too.

    We regularly do remote assistance with Microsoft, but I thought that such a
    basic thing as running a vbs from the Task Scheduler was not worth a ticket
    to Microsoft.

    wpher56, Mar 20, 2009
  16. I agree that this is a trivial issue. Unfortunately, despite of my best
    efforts, I haven't been able to establish a dialog that would have led us to
    a satisfactory solution. While you may have extensive experience in
    programming, your conclusions with this particular problem were repeatedly
    incorrect, starting with the very Subject of your post (Task Scheduler won't
    execute vbs). It actually did, as we established with a simple test. If you
    do not wish to make your machine visible to me (and I probably wouldn't
    either) then I suggest you raise another ticket with Microsoft. Remember to
    post the conclusion here for the benefit of everyone else!
    Pegasus [MVP], Mar 23, 2009
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.