Link to home
Start Free TrialLog in
Avatar of rsnellman
rsnellmanFlag for United States of America

asked on

Need batch script to accessing a mapped drive without logging into the system prior to running

Hi,
I am trying to figure out how to / best approach to setting up a batch script running without logging into the computer prior.

I have a batch script running ROBOCOPY commands to copy (sync) data from the local server to a remote file server via a network drive.

Both servers (Windows Server 2016 - Standard) reside on the same domain.


However, if someone unintentionally logs that account out then the script, obviously, fails due to not finding the network drive (destination).


How can I or would you resolve this issue?  UNC?  PS?


Examples would be greatly appreciated too.


Thanks in advance.
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

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 rsnellman

ASKER

OK. Thanks can you provide an example of the what the batch script might look like using UNC paths with say ROBOCOPY?


Example of my ROBOCOPY bat script:

robocopy "D:\Bkup Folder" "Z:\Bkup Folder" /MIR /R:10 /W:5 LOG:backup_log.txt



So, with that said, would the UNC paths coding look something like this?

robocopy \\SourceServer\Share \\DestinationServer\Share /MIR /R:10 /W:5 LOG:backup_log.txt



Thanks.
EXPERT CERTIFIED SOLUTION
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
OK.  When I manually run the script as your above example, it is successful.

Now we shall see if it runs tomorrow morning via Task Scheduler while NOT logged into the server.

(Fingers crossed.)

Well, Task Scheduler said the it ran the script successfully.  However, it completed in a few seconds, which I know it should take longer than that to complete the robocopy.

Also, I manually verified nothing new was copied to the destination (UNC path).

But when I manually run the Task Scheduler while, obviously, logged in, it actually runs correctly and completes, as well as, the destination location indicates the new files are copied there.


Any other suggestions?

Avatar of Hello There
Hello There

Open the properties of the task and make sure that the domain admin account is used and make sure that you tick "Run whether user is logged on or not" and "Run with highest privileges".
Well, did you have a look at the logfile?
OK. Hello There.  I did set those and verified.

oBdA,
Good point...duh.  I'll check to see what the log file says.


Thanks.

Well, strange.

The log file wasn't reflecting any updates to the script auto running last night.

Hmmm....

However, the log file was created when I manually ran the script outside of Task Scheduler.



The way it's set up, it will overwrite the log.
You can change the /LOG:"C:\Temp\backup_log.txt" to /LOG+:"C:\Temp\backup_log.txt" (note the + after /LOG) to let it add.
Let the task run again by the scheduler, then check the log before running it manually ....
But when I manually run the Task Scheduler while, obviously, logged in, it actually runs correctly
You are almost there. Now edit a trigger to run the task in 2 minutes, stay logged in, and see if it runs correctly (automatically).

If yes, it means that the task is configured correctly. I know that it doesn't work when the user is logged off so I would open the properties of the task, switch to "Run only when user is logged on", save changes, then open the properties again and switch back to "Run whether user is logged on or not".

If this won't help, I would delete the task and recreate it from scratch. Then edit the trigger as above, log out before the task runs and see if there is any progress.


Well, good news...it seems to have ran successfully last night WITHOUT being logged in.

However, it didn't update the log file (aka txt file).

Yes, I am aware of the /log+ parameter and it has worked on other servers in the past, but not sure why it isn't on this one.  The only difference of past servers and this one is the past servers were logged in when the scripts ran.

Anyways, I manually verified the robocopy (sync) was successful compared to the two locations (source & destination).


The log file not updating isn't the end of the world, but it is nice to have if issues happen.


I'll check it again in the morning to see if the results were consistent.


Stay tune...


Thanks.

OK.  So, I have success.

Once I modified the existing bat scripts to use the UNC path for the destination and changed the path of the log file to be saved under C:\Temp rather than just the root (C:\) it is running flawlessly without required any logged in accounts, as well as, updating the log file (.txt) in said location.


Thanks to all for your great amazing wonderful awesome assistance.


Take care and stay well.