2009-05-22

How to use Windows Task Scheduler to copy files from a server in another domain

My problem was that I wanted to do a nightly transfer of files froma Windows machine in another domain to my Windows Server 2003 machine.The script to do the file transfer was easy, but I couldn't find a wayto keep the remote server mapped as a network drive. If I mapped thedrive through the GUI and clicked "Reconnect at Logon" it wouldn'treconnect after the server rebooted. Possible part of what made itdifficult was that the remote server was in a different domain and Ihad a different credentials on the remote server.

I figured out a way to permanently map a network drive to the remote server in a different domain, but then I discovered that batch files run using the Task Scheduler do not see any mapped network drives. Apparently network drives are only mapped to drive letters for real interactive sessions, and a user's network drives are not mapped when you run run a Task Scheduler job as that user.

After poking around a bit I found that the solution was to enter my credentials for the remote server in the Stored User Names and Passwords (Start button -> Control Panel -> Stored User Names and Passwords) utility on the local server. Apparently if you do that, whenever the local machine tries to access a resource on the remote machine Windows automatically uses the stored credentials to log into the remote machine.

So, once I entered my credentials to the remote machine in Stored User Names and Passwords I then mapped a drive letter to that machine in the first line of the batch file like so:

net use n: \\servername\volumename /persistent:no

And then used that drive letter for the copy command in the batch file.

UPDATE 2009-09-04. Somehow my entries in Stored User Names and Passwords on a Windows Server 2003 machine vanished overnight. When I went into the Stored User Names and Passwords GUI application from the Control Panel and re-entered my credentials using the format "\\10.10.10.1\sharename" for the server name the stored credentials wouldn't work when I tried to access shares on remote servers. I then opened a DOS prompt and tried this:

net use \\10.10.10.1\sharename /savecred

which resulted in prompts for me to enter my user name and password, which I did. After that new entries showed up in the Stored User Names and Passwords panel access through the Control Panel, and the stored credentials worked when I tried connecting to remote servers. I did notice that the new entries created through the/savecred method used just 10.10.10.1 as the server name, with no slashes and no share name, so maybe that was the problem all along.

5 comments:

Anonymous said...

Thanks for posting this. It took me forever to find out why the script would work except for copying over the files to my mapped drive.

Anonymous said...

Your post is still helping desperate Windows users. For months I have searched for an answer to why a backup process would not post logs on the mapped network drive properly. All the documentation was useless, everything done correctly still no logs posted. It was about savcred.

Thank you

Anonymous said...

Thanks for the help! I searched lots for a solution to this problem and yours was the simplist and clearest.

I also found another solution, add/create a user on the target server/domain with the same name & password as the user running the job/command. For example, if you're running the command or batch file as \\mypc\john then create a user on the destination server as \\nas-share\john. It's important to use the same password too, that way when windows runs the copy command it can use the current credentials to log in to the target.

This might not always be possible as you may have different users running the command, but if you're scheduling it in Task Scheduler you can use the 'use the following account' and 'Run whether user is logged in or not' to control which user account is used.

Anyway, thanks again!

sam said...

My files and folder everything is in C why am I still getting error running batch file via task schedule. I login to PC using network login such as in school or work.

Anonymous said...

Thank you so much for this post!

On my machine, which is a windows 7 machine, I had to go to control panel\Credential Manager and add the credential for the remote machine as a windows credential, not a generic credential.