Batch Copy Logs From Server At Multiple Intervals Using Task Scheduler

I came across a situation where a vendor wanted logs files from a server at around 2am in the morning to troubleshoot why an application wasn’t sending a specific email at that time. Problem was, I wasn’t planning on being up at that early in the morning to grab log files and I didn’t know exactly what time it was supposed to send it. The time was usually around 1:30am but sometimes, when it worked, I’ve seen it sent at 4:30am.

So the plan was to come up with a simple batch file to handle the copying of the logs at every hour between 11PM and 5AM to my local Win 7 desktop,

This required a few things:

  1. A batch file that would a) make a directory based on the current time b) copy the files into that directory
  2. A task to run the batch file, triggered at every hour between 11pm and 5am.
  3. Access to the server where the logs are kept

 

Batch File

My batch file consisted of  the following:

@echo off
SET datestmp=%date:~-4,4%%date:~-10,2%%date:~-7,2%
SET HOUR= %time:~-11,2%
CALL :TRIM %HOUR%
SET timestmp=%HOUR%_%time:~-8,2%
MKDIR logs\servername\%datestmp%_%timestmp%
@echo on
COPY \\servername\logs\email.log  logs\servername\%datestmp%_%timestmp%\ @echo off
GOTO EOF
:TRIM
SET HOUR=%*
:EOF

 

The above batch file first gets the date and time, formats it, and creates a directory with the date/time as part of the directory name. So if the date and time was 6/09/15 and the time was 2am, the directory would be called “20150609_2_00”

Then it takes the email.log file from the \\servername\logs\ folder and copies it to a local folder on my machine called logs\servername\20150609_2_00\, which would be relative to where the batch file is run from.

I named the above batch file copylogs.bat and saved it on my local desktop under the C:\temp folder.

I ran the batch file to make sure it works and instead it failed! The reason being, the batch file ran under my desktop login account. I have a separate admin login for the server. So I mapped to the server using my admin login first. With that connection being made, I reran the batch file, and sure enough, it worked.

Next up was creating the tasks to run the batch at certain intervals…

Task Scheduler

1. Start by Creating a Basic Task:

basic task

2. Give the task a name:

task name

3. Set your trigger. In my case I’m setting to “One time” and will give it a specific time to start (probably about 20 minutes into the future so that I can test to see if it will run properly). Later, I will schedule it to run at particular times throughout the night.

trigger

4. Set your action. In My case I’m starting a program (my batch file):

action

5. Here’s where things start to get tricky.

Since it’s a batch file, the Program/script I’m running is cmd (command prompt). The Additional arguments will be:

/k start “” “c:\temp\copylogs.bat”

And it’s starting in c:\temp (no quotes).

start

6. After saving the task, one last thing I had to do was go back, edit it, and Change User or Group, so that it ran under my server admin account, and Run whether I was logged in or not, saving my password.

final

After I finished, and tested it successfully, I went back and edited the task, adding more Triggers:

more triggers

That’s it! Hopefully someone else will find this useful.