Link to home
Start Free TrialLog in
Avatar of Dr_Fox
Dr_FoxFlag for United States of America

asked on

Server 2008 Task Scheduler not running batch file

This should be really simple, but its not.  I've created a test batch file which consists of the following and placed it in my root directory.  The result should create a .txt file at the end. I can manually execute it with no problem.

@echo off
echo %date% %time% %UserName% >> c:\test.txt

My problem is, when I try to run this via task scheduler in Server 2008 R2 (My web box) it gives me the OpCode of 2 (File not found).  In Task Scheduler, I've told it the start in folder of C:\.  In my AD Group Policy, I've given permission to Log on as batch job to my account (Administrator) and did a gpupdate.exe /force, my event viewer for Task scheduler says it finished, however it returns the code of 2.

Any suggestions?

I've tried this test with .vbs, .CMD, and with ps1 examples all to no avail.
ASKER CERTIFIED SOLUTION
Avatar of Waseems
Waseems
Flag of Syrian Arab Republic image

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
Avatar of Dr_Fox

ASKER

Waseems, this did the trick for my simple .bat file.  I will mark you as the accepted solution.  For others that are having challenges with this, I have a .vbs that I created that starts and stops my IIS services on my IIS 7.0 web server.  In order to make that work, there were a few more hoops to jump through.

1.  Make sure you aren't running and storing your script in the root of C.
2.  Turn off UAC, UAC prevented me from being able to run my scripts when unattended.
3.  If you are running Active Directory, make sure you make your services account has access in the Group Policy Management Editor for Log on as a batch job that can be found under Computer Configuration/Policies/Windows Settings/Security Settings/Local Policies/User Rights Assignment.  If you are not using AD, then in gpedit.msc on the local machine, you have to set the service account Log on as a batch job in Local Computer Policy/Computer Configuration/Security Settings/Local Policies/User Rights Assignment.  Don't forget to do a gpupdate /force to foce your group policies to update.
4.  When creating your Task, the field for Start In (Optional) is not really optional, put in the path of your script location, otherwise it will give you errors of it can't find the file specified.
5.  If your using a batch or CMD file to fire off your script, make sure you put in an error code, otherwise Task Scheduler will not know that the process is done.  SET ERRORLEV=0 or EXIT 0.  This way Task Scheduler will stop running the script/batch/cmd file after it has been executed.

I believe that was it.  Hopefully my post will help others as well.