Windows 2008 R2 Task Scheduler fails to execute commands - batch-file

On my Windows Server 2008 R2 machine, a batch file is scheduled to run using Task Scheduler. For some reason, only a portion of the commands in the batch file are executed; Some commands are ignored.
Batch file looks like this:
cd s:
cd "S:\Reports\"
sqlcmd -S ipAddress -U login -P password -d dbname -i "S:\query.sql" -o "S:\Folder\Report.txt"
copy /A /Y "S:\Folder\Report.txt" Y:\
When I run the batch file by double-clicking on it, everything runs as intended; But when it is executed from task scheduler, only the sqlcmd portion runs correctly. The copy command does not seem to run. Even when I replace copy command with xcopy or mkdir or any other command, it does not run.
Even if I change the order of commands in the batch file, the commands do not run. But when the batch file is executed by double-clicking, everything runs perfectly.
The Y:\ is a mapped drive under the same login. After typing my question this far, I went back and ran one more test. Interestingly, if I replace mapped drive with local drive, everything seems to be working okay. So, at this point it looks like mapped drive is the problem. Any ideas on resolving this?
Essentially, how to copy or move files to a mapped drive (different machine) using batch file executed from Task Scheduler?

S: and Y: are probably mapped network drives, but those are mapped in the context of a user session. The scheduled task will be run in its own session and won't have those drives mapped.
You should use the UNC path of the share instead (\\server\share) as that should work in either context (as long as the share is accessible to the user the scheduled task is running as).

Related

How to map drive as system account via batch file

I need to map a network drive as a the local system account on a server, I can achieve this fine by running psexec -I -s cmd.exe then the net use command
My issue is the drive disconnects on reboot, even with persistent set to /p:yes
I’ve been trying to create a batch file that will run at startup that will run cmd via psexec, and then execute the net use command.
I can get to this point where cmd runs but I can’t get the next part of the command for net use to appear in the new cmd window.
Has anyone got any ideas/a better way of doing this altogether?
As a side note, the network share requires separate credentials to connect to it so it has to be mapped in such a way that credentials can be specified.
Have you tried saving the file as a .cmd file type and placing it in the shell:startup folder? It will run automatically every time you log in to the pc.

Scheduled Task Runs .bat File with sqlcmd, but Doesn't Update the Output File

My scheduled task (on Windows Server 2008 R2) is "successfully" running each morning, but the output of sqlcmd does not get updated in the output file. The log file changes to show that the batch file ran, but the output file remains the same.
When I run a .bat file with the lines of code below, it works just fine. The .csv file gets updated with the most recent query's results.
sqlcmd -S SERVERNAME -i my_query.sql -s "," -o c:\scripts\My_query\query_results.csv -W -h-1
ftp.exe -s:"c:\scripts\My_query\file_upload.ftp"
Useful Information:
I am running the scheduled task as my user which is a server admin and has access to the server in question
I have tried giving explicit access to the user for full control of all files and folders involved
I have tried running C:\Windows\System32\cmd.exe as the action with the argument /c C:\scripts\My_query\my_process.bat to make sure the entire process runs
Task scheduler is working fine for other tasks, but it has this same problem with five similar processes
Are the tasks configured to run whether the user is logged on or not? If so, it will never find your SQL file because the default working directory is SYSTEM32 when you configure a scheduled task that way. You will need to provide the full path to the SQL file.

Automatically run .bat file after EC2 reboot without having to remote connect

I am running an AWS Windows 2012 EC2 instance that has to run 24/7. On this instance, I run a Python 3.6 scraper script and to prevent me from having to regularly check up on the server whether the file is running, I have a .bat file in the shell:startup folder of my instance, that automatically restarts it on a daily base. The .bat file works as it will run the Python script and set a timer to restart/reboot the instance after (t=86400). The .bat file runs on the EC2 instance itself.
However, what the file does not do is run automatically after the reboot. I now first have to remote connect to the server before the .bat file will run. What I want it to do is run without me having to first remote connect into the server. How can I achieve this?
I use the following code in my .bat file. Located on my EC2 instance.
#ECHO OFF
START CMD /K (
CD C:/Users/Administrator/Documents/
python scraper.py
)
START CMD /K SHUTDOWN -t 86400 -r -f
I have tried looking into using AWS' Automations and other schedule based methods but couldn't get that to work.
If you want to use something native to Windows Server 2012, look at Schtasks -- this is more or less the Windows equivalent of cron.
I found the answer to my question by using Task Scheduler and looking at the following article: Run a batch file with Windows task scheduler
An important note here is that for my batch file to run I had to create a task that started CMD and run the batch file from there. Asking Task Scheduler to run the batch file directly doesn't work on Windows Server 2012. I ran the task with the following details:
Administrator account
"Run whether user is logged on or not"
"Run with the highest privileges"
"Start on system start-up"
Action: Start a program -> CMD
Add arguments (optional): /c start "" "C:\Users\Administrator\Desktop\file.bat"
More information on how to do this can be found in this answer: https://stackoverflow.com/a/27055435/7736676

Using Task Scheduler to run a batch file that contains the start /w command

I have a Windows XP virtual machine that I am using to run a legacy app. I am using a limited user account.
I have created a batch file that I'd like to:
Start the legacy app
Wait for the program in step 1 to close
Shut down the computer
So this is the code I am using (in this example, I am attempting to open notepad):
#ECHO OFF
start /w notepad
%windir%\system32\shutdown.exe -f -s -t 1
Because the legacy app needs to be run under an account with administrator privileges, I then manually created a scheduled task in Task Scheduler to run the batch file. The scheduled task is set to run using an account named Admin that has administrator privileges.
I am having a bit of an issue. When the scheduled task runs, nothing appears on the screen. However, I did notice that both cmd.exe and notepad.exe appear in Task Manager as processes running as the user Admin.
What am I doing wrong? How can I accomplish what I'd like to do? Thanks!
Sorry all, it was an issue with me not having Task Scheduler set up properly. I found this post which suggests to use the AT command to run the task interactively. This allows the Cmd window to be displayed when a batch file is run as a Scheduled Task.

Batch runs manually but not in scheduled task

I don't generally write batches, but I currently have a batch that uses forfiles to copy my FLVs from one folder to another. When I run the batch manually it works every time, but from a scheduled task, it throws a (0x1) error.
forfiles -p "C:\Program Files\Adobe\Flash Media Server 4.5\applications\name\streams" -m *.flv -s -d -1 -c "cmd /c copy #file ^0x22C:\Program^ Files\Adobe\Flash^ Media^ Server^ 4.5\applications\name\output\"
Not sure what syntax the scheduled task doesn't like.
Update
Under my scheduled task, Actions I have the following:
Program/script: name.bat
Start in (optional): \\servername\file\to\batch
Hi might be this is helpful,
I also face the same issue.
Just set the startin path like:
Here start in path is the path of batch file:
like you have enter in program script:
"E:\program related files\demo.bat"
then in startin just pass:
E:\program related files & done!
When my Start in (optional): path was a UNC path, it wouldn't work. So I moved my batch on the server and everything worked correctly.
UNC in Windows Batch Files
forfiles with UNC path
On Server 2008 R2 when running the batch file under domain user credentials, with confirmed "log on as a batch job" security in the Local Security Policy>Local Settings>User Rights Assignment,
even then my batch (copying a log file to a network share) would not run as scheduled task, until I selected in tab General the option "Run with highest privileges" (default NOT checked!)
The option Run whether user is logged on or not was also selected, with radio-button, but I guess this is quite standard, when selecting to run the task using a domain user account.
For the tab Actions : specifying the entire batch file name including its path, directly in "Program/script:" works fine (with Server 2008 R2)
Using double quotes inside the batch file causes no problems.
See the screen shot bellow.
You need to change the user to system
Most common reason for such problems is permissions: scheduled tasks does NOT always run with your user credentials. If you want scheduled task to run as you you will have to set it up as you or alternative user.
Besides that I hope that your line of code is a content of your batch file, you are not trying to run this command directly. Or are you?
P.S. What are these ^0x22 and ^ doing in your code?
I know this is an old question, but just wanted to share some info.
The (0x01) error code can also refer to resources that are not found. Therefore:
all files/folders referd from within the batch file should be accessible to the user which account is being used to run that scheduled task;
pay attention when using network locations in combination with "Run whether user is logged on or not" option;
the above-mentioned option can be tricky to use because some resources may be available only after log on.
For .bat files to run inside your scheduled task, you need to specify your .bat file path inside the start option - despite the fact that your .bat file is at the same directory as your .exe. Also, I flagged it to run with highest privilege. After I have done those two things, the task suddenly takes off without any problem!

Resources