Link to home
Start Free TrialLog in
Avatar of sparker1970
sparker1970Flag for United States of America

asked on

Problems with Windows Task Scheduler

I have a number of tasks that I schedule to run on our server every morning. I had some problems with the batch files executing when it was setup to run whether or not the user was logged in so my scripts are setup to run only when the user is logged in. I run into a problem on Saturdays and workweek holidays when the user is NOT logged in for a variety of reasons.

The user the task is trying to run under is the administrator login and has full permissions.

For the last few months I've had the scripts set to run only when the user is logged in which is fine 90% of the time. I'm getting tired of adjusting my scripts because they did not run the previous day because the user was not logged in. The research I have done so far has indicated that there is a problem with Windows Server R2 SP1 that will not allow the tasks to run using the setting that will log the user in if it does not happen to be logged in.

My research has uncovered a number of "recommended solutions". I've seen information about adjusting permissions on cmd.exe as well as "patches" from Microsoft for a known issue.

The task scheduler does not indicate an error when the script fails to launch when it is set to "run whether users is logged in or not". The task simply says "running" and it appears in the running processes in the task list but never gets to step #1 of the script.

Please help...my current fallback is to develop "Monday scripts" so I can setup scripts to run to pull system statistics for Friday using TODAY-3 for a date instead of my TODAY-1 I use for tasks launched daily.
Avatar of Jeremy Weisinger
Jeremy Weisinger

I have not had issues running script in the task scheduler when the user is not logged on. There are certain commands that require the user context that shouldn't be used in scheduled tasks if you want them to run when the user isn't logged on. Could you post the script and we might be able to see where it would be hanging up?
First... you probably don't want to run scheduled tasks as your administrator.

My recommendation would be to create another account... a service account used just for tasks. That account may still be a member of the local administrators group, but you should also go into your policy editor and select User Rights Assignments, and allow that account to "Log on as a batch job" and "Log on as a service".
Avatar of sparker1970

ASKER

As a follow up to the 2 replies so far...

The script runs perfectly when run manually by double-clicking the bat file. The job also runs when run from within the task scheduler. The commands and coding within the bat file is correct and is not the source of the problem (I have 15 different scripts running a variety of tasks that run into the same issue). The problem is specific to the fact the scheduler will not launch a session when the setting is to run whether or not the user is logged in. Jobs execute fine when the user is logged in.

With regards to using the administrator account for running batch jobs...I understand completely the pros and cons of doing this and I am planning to create a specific user for scheduled tasks once I know the scheduler is working. Right now our servers reboot each night launching the administrator account on reboot and allowing my scripts to run the next morning. It would be an extra manual step to have another account logged in 24/7 to keep these scripts running. If I get the tasks to work and login as a different user when "user is not logged in" I will have many more options as to alternative user accounts to use.

Right now the problem is simple, I cannot get the task scheduler to work when the setting is to "run whether user is logged in or not". This is the solution I am looking for.
If the script is "simply running" when it tries to execute when the user is not logged in then the issue is with the script itself. If you would like to post the script or give us an idea of what the script is, then we might be able to help you troubleshoot it.
OK, well if you want to continue running the script with the current account, then go into secpol.msc and go into Security, and then User Rights Assignments, and make sure that account has the right to "Log on as a batch job".
Here is one of the scripts:

1 move "N:\Scheduled Reports\RPC Report\WEBE_DISCV_*.xlsx" "N:\Scheduled Reports\RPC Report\OldReports\"

2 "C:\Program Files (x86)\Microsoft Office\Office12\MSACCESS.exe" "N:\Client Reporting\Client Database.accdb" /x ClientRPC_(Daily)

**The line numbers are for reference only and are not in the actual bat file.

There are 2 lines of "code". The first moves files from the top folder in a tree and the second line opens Access and launches a macro.

Like I said. This script executes as expected when the user is logged in, when run manually thru the scheduler, or run manually outside of the scheduler.
Running these while you are logged in uses the security context of the account you are logged in with. Trying to execute these as a scheduled task removes that security context, and some of what you are doing requires it to be there. That's why allowing the account to log on as a batch job should help, because it tells the scheduler to log on to perform the batch job, thereby loading the security context.
Is N:\ a local or mapped drive? If it's a mapped drive then that is why your script only runs when you're logged on. You'll need to use the UNC path.
I log on to the computer as the administrator when I set up the tasks. I am also logged in as the administrator when I am testing the batch script using the scheduler. It should be the exact same security profile that executes the script. Since everything is working except when the user is not logged on I suspect the problem exists within windows scheduler and it not logging in with the user credentials I provide when setting up the task.

Granted I'm not an expert by any stretch but if I am logged onto the computer as the administrator and the script works, and the script works from the scheduler when the administrator is logged in, and the script works when run manually as the administrator or any other authorized user.

I see the problem as having something to do with the scheduler not properly logging in using the username and password provided since everything else works under the login when done manually or scheduled when user is logged in.
(twice)
ASKER CERTIFIED SOLUTION
Avatar of Jeremy Weisinger
Jeremy Weisinger

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Jeremy...I checked with one of my network guys and he confirmed that the normal scripts to map the network drives will not be launched by a user login from the task scheduler. I am working the mapping script into my batch scripts to properly map the drives and verify that works. I appreciate the simple way you explained it as well. The user does have all the correct security permissions to perform the tasks when logged in but those network drives are not mapped and the script does not "find" the database to open to run the macro. I will update this feed once I can test my logon script in conjunction with my current scripts.
Thanks for the simple explanation and not over-complicating your answer. The solution makes perfect sense and is something outside my area of knowledge since I rarely handle user accounts or login scripts.
Glad to help