Troubleshooting Scheduled Task Issues in Windows Server 2008 R2

batchscheduled-tasktroubleshootingwindows-server-2008-r2

Situation

I have a batch script that prepares some files, executes a program (.exe) and then deletes said files.

This task should run hourly, so I'm trying to configure this using Scheduled Tasks. The problem is that the previously mentioned program does not run properly when invoked from the task (neither via the .bat script, nor when calling the .exe directly), but I don't get any warning or error messages in the logs.

Setup

The task is configured to run as a Windows Service Account that has all the privileges set properly. When using this account to logon via RDP, I can execute the .bat and .exe directly without problems, but still the task appears to do nothing. This is easily observed because the program always modifies a file, and the modified on timestamp does not change through the task.

In the scheduled task logs I get the information messages for the task starting a process, exiting, etc. The "result code", however, is 111 (tried to Google this without luck, the only association I get is "file name is too long", which is just completely irrelevant AFAIK). In the application logs, I get absolutely nothing.

What I suspect is the problem

The program is an old monstrosity that spawns some sort of splash screen (it's actually a normal window), even though the GUI is not needed because it requires no interaction and closes itself after operations. The window appears for about 2 seconds.

I suspect that this requirement for a GUI has something to do with the task failing, but I'm not sure. When I log in with the user that the task runs under (via RDP), no window appears when I start the scheduled task.


Edit about the GUI

I've built a very small C# executable that launches the program without the main window (using ProcessStartInfo.WindowStyle = ProcessWindowStyle.Hidden). Even this way, the scheduled task still does not succeed to launch the program correctly, but the return code is now 0.


Update

When I configure the task to say "run whether the user is logged on or not", and the run with highest privileges option is unchecked, the error value is 2147943859.


What can I do to troubleshoot?

OS = Windows Server 2008 R2 SP1

If more info is needed, please let me know in the comments.

Best Answer

I believe your problem has to do with either the permissions of the account being used to run the task, or the context of the account as exists when trying to run the task.

Test for Console Session Requirement

It's possible your .EXE must be run in Console session (aka Session 0) on the computer. To test for this:

  1. Configure the task to Run only when user is logged on and specify a task start time of 2 minutes in the future
  2. Log on to the machine with the same user account used to run the task (preferably log on to the console session, either by physically being at the console or using a remote access program that gives access to the console. To confirm you are using the console session, from a Command Prompt run QWINSTA, observe the SESSIONNAME column, and confirm the > indicator is next to console, in other words it should appear as >console)
  3. Wait for the task to run

If the task runs correctly, try scheduling the task with SCHTASKS.EXE using the /IT parameter. Failing that, you may have no choice but to configure the computer to automatically log on as your service user account and run the task as a startup program.

Check Permissions

Additionally, as I've already suggested, check the following to confirm the account used to run the task is properly permissioned:

  1. Grant the account the Logon as a batch job user right (Found in Local Group Policy at Computer Configuration/Windows Settings/Security Settings/Local Policies/User Rights Assignments)
  2. Confirm the task is configured to Run with highest privileges
  3. Confirm the user has full NTFS permissions to all folders & files it must interact with. Make no assumptions; instead confirm by navigating to such file locations and using the Effective Permissions tab in the file/folder's Properties at Security > Advanced

Additional things to check/try

  • Does the task require access to access to network resources? Things like mapped drives may be present when you logon with the user account, but depending on the server's configuration may not be present in the context of the user account when executed from Task Scheduler.
  • Add some logging to your batch file. After every line it executes, have it write some output to a log file so you know where it's getting stuck. For example:

    @echo off
    echo Line 1 >> "C:\MyLog.txt"
    "C:\My Folder\myOldProgram.exe"
    echo Line 2 >> "C:\MyLog.txt"
    DEL somefile.dat
    echo Line 3 >> "C:\MyLog.txt"
    
  • Try running your .EXE with START, for example START "myTitle" "C:\full\path\to\my.EXE"