Task Scheduler Isn't Running Bat File Correctly

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
roystreet
Posts: 6
Joined: 16 Jun 2010 15:35

Task Scheduler Isn't Running Bat File Correctly

#1 Post by roystreet » 16 Jun 2010 15:55

Hello,
...I've been searching for the answer to this, but I can't find the answer & just maybe someone here can answer it? I use Win 7 :D - I have a bat file that maps a temporary "admin" drive & then runs a bat file at that location. That bat file then maps other drives if necessary & performs other actions.

When I run the bat file manually, it works perfectly. When I place it in the startup folder, it runs perfectly everytime the user logs on. No problem. When I use the task scheduler, it starts the (local) bat file, then it starts to run the network bat file, but then after that it seems to fail to map the drives as needed & fails to perform other actions.

Now, I have the same type of bat file running in Vista & it works perfectly with task manager.

Now, I wouldn't mind using some other method of scheduling...This bat file needs to run on logon & then hourly while user is logged on. But...I would like to understand why it's not working.

(Why do I design this bat to call another on a network location you might ask? - It is on a server in which I can just update the file on the server to make changes to end users machine the next time their bat file runs. It is a linux server)

Note: I have ensured that the file & the task itself runs with the highest rights/privledges possible. I have tried placing quotes before/after the command to run in the task which made no difference.
Thanks for your time & consideration,
~roystreet

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: Task Scheduler Isn't Running Bat File Correctly

#2 Post by amel27 » 16 Jun 2010 19:26

roystreet wrote:But...I would like to understand why it's not working.
get detailed error message of command - schedule/run Bat with redirecting all output to file and then view this log:

Code: Select all

script.cmd >>c:\script.log 2>&1


if present "@echo off" command, change to "@echo on" or comment

roystreet
Posts: 6
Joined: 16 Jun 2010 15:35

Re: Task Scheduler Isn't Running Bat File Correctly

#3 Post by roystreet » 16 Jun 2010 19:56

Hi - I will try what you said, but before I do I just wanted to offer a little bit more of what I'm finding it's doing. In the file it should map a the "S" (Admin) drive. It appears to be doing it since it's running a bat file located there, but...It doesn't show it's mapped when I look at my computer. When I run it manually it shows it mapped. I'm not sure how it's running a file in that location even though it's showing it's not mapped. So I test drove another bat file that I made real quick

net use X: \\server\share
pause

OK, it should map X & pause waiting for my input. It states it's done, but before I press enter to complete it, I look to see if X is mapped. It isn't. So I press enter so that the file closes - Still no X. If I run that simple bat file manually, it maps X just fine & dandy with no problemo! - How could this be?

The problem persists when using task scheduler - otherwise they run fine
I will still do what you said here in a little bit.

Thanks
~roystreet

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: Task Scheduler Isn't Running Bat File Correctly

#4 Post by amel27 » 16 Jun 2010 22:38

roystreet wrote:It appears to be doing it since it's running a bat file located there, but...It doesn't show it's mapped when I look at my computer.
this bat files run on different separate sessions with its own map drives

roystreet wrote:The problem persists when using task scheduler - otherwise they run fine
no, for example, launch CMD or BAT in administrative mode (UAC), map drive via "NET USE S:", then execute "S:"... and compare with "My Computer"... ;)

roystreet
Posts: 6
Joined: 16 Jun 2010 15:35

Re: Task Scheduler Isn't Running Bat File Correctly

#5 Post by roystreet » 17 Jun 2010 09:52

amel27 wrote:
roystreet wrote:It appears to be doing it since it's running a bat file located there, but...It doesn't show it's mapped when I look at my computer.
this bat files run on different separate sessions with its own map drives

roystreet wrote:The problem persists when using task scheduler - otherwise they run fine
no, for example, launch CMD or BAT in administrative mode (UAC), map drive via "NET USE S:", then execute "S:"... and compare with "My Computer"... ;)


So if I just run the bat file manually, it's different than when it's run via the scheduler? I will see it map when I run it manually. Thank you for that incite, that is valuable information. Just so I understand you correctly, when the Task Scheduler is running, it runs the bat file in admin mode? Why does it work in XP or Vista though? I see "S" mapped.

This could be an important clue to help me understand this better.
~roystreet

roystreet
Posts: 6
Joined: 16 Jun 2010 15:35

Re: Task Scheduler Isn't Running Bat File Correctly

#6 Post by roystreet » 17 Jun 2010 19:59

amel27 wrote:
roystreet wrote:But...I would like to understand why it's not working.
get detailed error message of command - schedule/run Bat with redirecting all output to file and then view this log:

Code: Select all

script.cmd >>c:\script.log 2>&1


if present "@echo off" command, change to "@echo on" or comment


Hello,
...I've tried @echo on & thus I can see the bat file as it's working. The "script.cmd >>C:\script.log 2>&1" appears to create script.log & in that log it states:
'script.cmd' is not recognized as an internal or external command, operable program or batch file.


...I put it right at under @echo on statement in the first bat file. Am I missing something? I appreciate your help. Again, I run it manually & it does everything it should do.

~roystreet

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: Task Scheduler Isn't Running Bat File Correctly

#7 Post by amel27 » 18 Jun 2010 01:22

roystreet wrote:...I put it right at under @echo on statement in the first bat file. Am I missing something?

instead of "script.cmd" substitute name of your script for scheduling (from 1'st post)... simply add command line parameters for this script: ">>c:\script.log 2>&1" to scheduler, start task and find "c:\script.log" file

roystreet
Posts: 6
Joined: 16 Jun 2010 15:35

Re: Task Scheduler Isn't Running Bat File Correctly

#8 Post by roystreet » 19 Jun 2010 20:53

:( Hello...I want to say I appreciate your patience with me. I obviously thought that I knew more than I do concerning bat files. I have placed

Code: Select all

myfile.bat >>c:\script.log 2>&1
right under @echo on. I have placed it there with quotes & without quotes. :| I have also placed the full path & name of the file & then the

Code: Select all

>>c:\script.log 2>&1
.

When I place the name of the bat file, it will generate an error in the script.log file stating:
'myfile.bat' is not recognized as an internal or external command, operable program or batch file.


If I place the full path name, it will generate what seems to be an endless script.log file stating over & over:
C:\Windows\system32>C:\Users\fred\Downloads\BAT-Files\myfile.bat 1>>c:\script.log 2>&1


If I place

Code: Select all

cd C:\Users\JKR\Downloads\BAT-Files
& then the next line

Code: Select all

myfile.bat >>c:\script.log 2>&1

I likewise will get an endless file which states:
C:\Users\fred\Downloads\BAT-Files>cd C:\Users\fred\Downloads\BAT-Files

C:\Users\fred\Downloads\BAT-Files>myfile.bat 1>>c:\script.log 2>&1


I certainly am confused & my suspicion is that I'm misunderstanding what you need me to do. I'm sorry for making this difficult for you.

~roystreet

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: Task Scheduler Isn't Running Bat File Correctly

#9 Post by amel27 » 20 Jun 2010 04:39

ok, step by step:

- open original (local) bat-file for editing;
- find and replace line "@echo off" to "@echo on";
- find line where start (network) bat file, it must start via CALL command;
- save& close local bat-file;

- open network bat file for editing;
- find and replace line "@echo off" to "@echo on"
- save& close network bat-file;

- goto task scheduler, open "Properties" for task of local bat file;
- goto "Actions" tab & press "Edit" button for "Start program";
- fill in field "Add arguments (optional)" with ">%userprofile%\script.log 2>&1";
- fill in field "Start in (optional)" with folder path, where local bat file is;
- click "Ok", verify command line ("Details" field) and click "Ok" again;

- Run Job;
- GoTo %UserProfile% folder (usually "C:\Users\UserName") and find script.log file

roystreet
Posts: 6
Joined: 16 Jun 2010 15:35

Re: Task Scheduler Isn't Running Bat File Correctly

#10 Post by roystreet » 20 Jun 2010 22:28

OK, I simplified my bat script to only map drive "S" instead of the rest of the actions.
You see below how I map the drive...It states that it's already in use, even though it doesn't appear it's being used anywhere.

C:\Users\fred\Downloads\BAT-Files>NET USE S: \\SERVER\netty\star /persistent:no
System error 85 has occurred.

The local device name is already in use.


If I tell it to 1ST disconnect drive s before mapping it (as shown below), it displays this:

C:\Users\fred\Downloads\BAT-Files>Net use s:\ /delete /y
The network connection could not be found.

More help is available by typing NET HELPMSG 2250.


C:\Users\fred\Downloads\BAT-Files>NET USE S: \\SERVER\netty\star /persistent:no
System error 85 has occurred.

The local device name is already in use.


So, if I try to map it..."It's already in use"...If I try to disconnect it..."The connection could not be found"...The same connection in which says is being used already. :|

Also when the bat script is running, the cmd window opens, but no text is shown...Is it because it's displaying in the log file instead?

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: Task Scheduler Isn't Running Bat File Correctly

#11 Post by amel27 » 21 Jun 2010 01:46

roystreet wrote:So, if I try to map it..."It's already in use"...If I try to disconnect it..."The connection could not be found"...The same connection in which says is being used already.
it is not problem of bat file, but problem of Windows/Explorer, your may found set of cases via google - when user account start more than one session simultaneously (RunAs, Scheduler, Terminal, etc.)... Try test another drive letter, delete/create user profile and so on... I would recommend create separate user account for this scheduled job.

roystreet wrote:Also when the bat script is running, the cmd window opens, but no text is shown...Is it because it's displaying in the log file instead?
yes, switch ">" redirect all output to file, it can be useful for troubleshooting non-interactive command sessions

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Task Scheduler Isn't Running Bat File Correctly

#12 Post by Samir » 16 Jul 2013 12:32

roystreet wrote:OK, I simplified my bat script to only map drive "S" instead of the rest of the actions.
You see below how I map the drive...It states that it's already in use, even though it doesn't appear it's being used anywhere.

C:\Users\fred\Downloads\BAT-Files>NET USE S: \\SERVER\netty\star /persistent:no
System error 85 has occurred.

The local device name is already in use.


If I tell it to 1ST disconnect drive s before mapping it (as shown below), it displays this:

C:\Users\fred\Downloads\BAT-Files>Net use s:\ /delete /y
The network connection could not be found.

More help is available by typing NET HELPMSG 2250.


C:\Users\fred\Downloads\BAT-Files>NET USE S: \\SERVER\netty\star /persistent:no
System error 85 has occurred.

The local device name is already in use.


So, if I try to map it..."It's already in use"...If I try to disconnect it..."The connection could not be found"...The same connection in which says is being used already. :|

Also when the bat script is running, the cmd window opens, but no text is shown...Is it because it's displaying in the log file instead?
Check your syntax on the net disconnect as I believe you only put the drive letter, not the \ for the path.

Post Reply