windows 如果通过计划任务调用 BAT 文件,则无法将文件复制到 UNC 目标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10175893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 09:27:27  来源:igfitidea点击:

Can't copy files to UNC Destinations if BAT file is called via scheduled task

windowsnetworkingcopywindows-server-2008

提问by Imran

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

我有一个 bat 文件将文件从当前机器复制到映射的网络驱动器(一行,xcopy 命令)。

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.

当我 RDP 到服务器时它工作。但是,当我作为计划任务运行并将其配置为在我登录的同一用户下运行时,它不起作用并给出错误 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.

我也尝试了 dsynchronize,当我单击同步时它可以工作。当我将它作为服务运行时,同样的问题。

回答by Imran

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

它每次都映射一个网络驱动器,然后复制到该驱动器

回答by Thorsten Hüglin

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.

可以将文件复制到 UNC 路径而无需映射为网络驱动器。只需尝试在引号中设置 UNC 路径。

copy * "\server\share"

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

没有引号,我在 Windows7 命令行上运行时出现“语法错误”。

回答by Gene Mat

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.

我遇到了类似的问题,我想将文件从服务器复制到数百个其他服务器,而无需将远程驱动器映射到本地 PC。我没有足够的驱动器号来将数百台远程机器映射到我的本地 PC!我不能只是映射远程驱动器并复制。

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.

我想我可以使用 copy、xcopy 或 robocopy,并将我的凭据指定给 copy 命令。但是没有一个复制命令有任何选项可以向远程系统提供凭据。

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
)

回答by Benedict

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.

我有一个类似的问题,我只需要将密码存储为计划任务的一部分,而不是使用 net use。你会注意到它说如果它被勾选,它只能访问本地资源。

Store password

存储密码

回答by MSalters

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.

谁映射网络驱动器?您是否使用映射名称,而不是底层 UNC 本机路径?因为听起来映射驱动器是在您的登录脚本中设置的,如果您未登录,则该脚本不会运行。因此,在计划任务中,您确实拥有 UNC 路径的正确凭据,但没有映射驱动器号.