BAT file will not run from Task Scheduler but will from Command Line

22,005

If batch files are failing to execute as a scheduled task but running fine interactively, it most probably is due to a security / permissions issue. The SYSTEM / LocalSystem account which is often used to run scheduled tasks usually does not have permissions to access any networked resources for example.

What is the security context of your scheduled task? You could try changing it to something more privileged (like a domain administrator account) temporarily as a hypothesis test.

If it works, you should create a new account with a complex password and sufficient privileges to use with tasks. The account password is saved along with the scheduled task - by using a separate account you are following a good security practice and preventing failing task runs whenever the administrator's password changes.

Share:
22,005

Related videos on Youtube

wtaylor
Author by

wtaylor

Updated on September 18, 2022

Comments

  • wtaylor
    wtaylor over 1 year

    I'm trying to run a BAT script from Task Scheduler in Windows 2008 R2 and it runs for 3 seconds and then stops.

    It says it successfully completes but I know it doesn't.

    I can run this script from the command line directly, and it runs just fine.

    The bat file I'm running actually deletes files older than 7 days using "forfiles" then I'm mapping a network drive, moving the files across the network using robocopy, and then closing the network connection.

    I have taken the network and copy options out of the file and it still does the same thing. Here is how my file looks:

    rem This will delete the files from BBLEARN_stats
    forfiles -p "E:\BB_Maintenance_Data\DB_Backups\BBLEARN_stats" -m *.* -d -17 -c "cmd /c del @file"
    
    rem This will delete the files from BBLEARN_cms_doc
    forfiles -p "E:\BB_Maintenance_Data\DB_Backups\BBLEARN_cms_doc" -m *.* -d -14 -c "cmd /c del @path"
    
    rem This will delete the files from BBLEARN_admin
    forfiles -p "E:\BB_Maintenance_Data\DB_Backups\BBLEARN_admin" -m *.* -d -10 -c "cmd /c del @path"
    
    rem This will delete the files from BBLEARN_cms
    forfiles -p "E:\BB_Maintenance_Data\DB_Backups\BBLEARN_cms" -m *.* -d -10 -c "cmd /c del @path"
    
    rem This will delete the files from attendance_bb
    forfiles -p "E:\BB_Maintenance_Data\DB_Backups\attendance_bb" -m *.* -d -10 -c "cmd /c del @path"
    
    rem This will delete the files from BBLearn
    forfiles -p "E:\BB_Maintenance_Data\DB_Backups\BBLEARN" -m *.* -d -18 -c "cmd /c del @path"
    
    rem This will delete the files from Logs
    forfiles -p "E:\BB_Maintenance_Data\logs" -m *.* -d -10 -c "cmd /c del @path"
    
    NET USE Z: \\10.20.102.225\coursebackups\BB_DB_Backups /user:cie oly2008
    
    ROBOCOPY E:\BB_Maintenance_Data Z: /e /XO /FFT /PURGE /NP /LOG:BB_DB_Backups.txt
    
    openfiles /disconnect /id *
    
    NET USE Z: /delete /y
    

    This is happening on 2 servers when trying to run commands from inside a BAT file.

    The other server is giving an error if (0xFFFFFFFF) but that file is running a CALL C:\dir\dir\file.bat -options and I've used commands like that before in Server 2003.

    Here is the file for this file:

    call C:\blackboard\apps\content-exchange\bin\batch_ImportExport.bat -f backup_batch_file.txt -l 1 -t archive
    
    NET USE Z: \\10.20.102.225\coursebackups\BB_Course_Backups /user:cie oly2008
    
    ROBOCOPY E:\ Z: /move /e /LOG+:BB_Move_Course_Backups.txt
    
    openfiles /disconnect /id *
    
    NET USE Z: /delete /y
    
    • MrGigu
      MrGigu over 12 years
      Why are you mapping the network drive? That's a user-interaction, not something you would normally include in an un-attended script. Can you just copy the files directly to the UNC path?
    • wtaylor
      wtaylor over 12 years
      I found this Hotfix, but even if I remove the "s it doesn't work correctly, but maybe this will help someone else support.microsoft.com/kb/951246.
    • wtaylor
      wtaylor over 12 years
      Even if i removed the networking stuff, it still errors out. I want this file to move files during the slow time of my server (IE: 1AM).
    • Philip
      Philip over 12 years
      So what is or isn't the batch file actually doing if not what it is supposed to do?
  • wtaylor
    wtaylor over 12 years
    I'm working the network admin (server admin who setup the server) and trying to get his help with permssions. I was able to fix the second BAT file by adding the folder to the "start in" option but it didn't fix the first BAT.