We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x
Private

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

High Priority
47 Views
Last Modified: 2020-05-20
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.
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
Use UNC paths instead of mapped drive letters, and schedule the script to run under an account with the necessary network permissions to access the share.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
rsnellmanIT Manager

Author

Commented:
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.
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
If D: is a local drive on the source machine, then you can continue using the drive letter. Just don't use drive letters for network drives.
For the target, you'll probably need the target folder as well.
Just run "net use" to check to which UNC path Z: is mapped to, then replace Z: with the UNC.
So if D: is local, and Z: is mapped to \\DestinationServer\Share, it should look like this (according to your original example).
And it's probably best to specify the full absolute path for the log file.
robocopy.exe "D:\Bkup Folder" "\\DestinationServer\Share\Bkup Folder" /MIR /R:10 /W:5 /LOG:"C:\Temp\backup_log.txt"

Open in new window

rsnellmanIT Manager

Author

Commented:
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.)

rsnellmanIT Manager

Author

Commented:
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?

Hello ThereSystem Administrator
CERTIFIED EXPERT
Distinguished Expert 2018

Commented:
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".
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018

Commented:
Well, did you have a look at the logfile?
rsnellmanIT Manager

Author

Commented:
OK. Hello There.  I did set those and verified.

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


Thanks.

rsnellmanIT Manager

Author

Commented:
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.



CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018

Commented:
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 ....
Hello ThereSystem Administrator
CERTIFIED EXPERT
Distinguished Expert 2018

Commented:
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.


rsnellmanIT Manager

Author

Commented:
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.

rsnellmanIT Manager

Author

Commented:
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.

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.