13

I have a bat file copying files from current machine to mapped network drive (one line, xcopy command).

It works when I RDP to server. However, when I run as a scheduled task, and configure it to run under the same user I'm logged in, it doesn't work and give error 0x4.

Is there a way I can achieve this?

I also try dsynchronize and it works when I click synchronized. When I run it as service same issue.

Imran
  • 656
  • 1
  • 7
  • 13
  • Upvoted the one that helped me below, but this answer also helped me debug it in my case http://stackoverflow.com/questions/8662024/how-do-i-capture-the-output-of-a-script-if-it-is-being-ran-by-the-task-scheduler – lessthanideal Apr 14 '14 at 16:11

5 Answers5

22

I was able to figure it out. Following batch files works under scheduler, even as local system account:

net use m: \\server\share /U:server\user password
xcopy C:\source m: /E /Y

It maps a network drive every time and then copy to that drive

Nolonar
  • 5,962
  • 3
  • 36
  • 55
Imran
  • 656
  • 1
  • 7
  • 13
10

It's possible to copy files to a UNC path without mapping as a network drive. Just try to set the UNC path in quotes.

copy * "\\server\share"

Without the quotes, i got a "syntax error" running on Windows7 command line.

Thorsten Hüglin
  • 1,217
  • 10
  • 11
5

I had similar issue where I wanted to copy file(s) from a server to hundreds of other servers without mapping a remote drive to my local PC. I didn't have enough drive letters to map hundreds of remote machines to my local PC! I couldn't just map the remote drive and copy.

I thought I could use copy, xcopy, or robocopy, and specify my creds to the copy command. But none of the copy commands had any options to provide credentials to remote system.

Thanks to the post above, I was able to create a small batch file where I just loop through my hosts, and keep re-using just one drive mapping for all my hosts.

Here is my batch file...

echo @off
for /F %%j in (pchostslist1.txt) do (
  net use z:\\%%j\c$ /user:domain\myusername mypassword
  mkdir \\%%j\c$\tmp\mynewdir
  xcopy c:\anyfile.txt \\%%j\c$\tmp\mynewdir
  net use z: /delete
)
Gene Mat
  • 66
  • 3
4

I had a similar issue and instead of using net use I simply needed to store the password as part of the scheduled task. You'll notice that it says it only has access to local resources if it's ticked.

Store password

Benedict
  • 190
  • 2
  • 7
1

Who maps the network drive? And are you using the mapped name, instead of the underlying UNC native path? Because it sounds like the mapped drive is setup in your login script, which doesn't run if you're not logged in. So, in a scheduled task, you do have the correct credentials for the UNC path, but no mapped drive letter.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • I maps the network drive. Yes I'm using mapped name. Scheduled task give the same error even if I'm logged in – Imran Apr 16 '12 at 16:35
  • 2
    It doesn't matter whether you're logged in for the scheduled task. It uses its own session, and the drive isn't mapped in that session. – MSalters Apr 17 '12 at 07:25
  • @Marc: Of course it does, as the comments confirm. The symptom is not being able to copy the file, the question is why. The answer is a missing drive letter for a UNC path because a login script isn't run. – MSalters Feb 26 '16 at 15:12