简体   繁体   English

Windows调度程序未执行批处理脚本,但是该脚本可以从命令行正常运行

[英]Windows scheduler isn't executing the batch script,but script works fine from command line

I want to create folder with todays date as the name of folder. 我想用今天的日期创建文件夹。 Then, I want to move the data from remote machine to the newly created folder. 然后,我想将数据从远程计算机移至新创建的文件夹。

I have written a batch script which looks like 我写了一个批处理脚本,看起来像

My batch script. 我的批处理脚本。
name: run.bat 名称:run.bat

::@ECHO off
SET CurrentDate= %date:~-4,4%_%date:~-10,2%_%date:~7,2%
SET CurrentDate=%CurrentDate: =%
"%SystemRoot%\System32\cmd.exe"  /c mkdir  "Z:\some_name_commercial\%CurrentDate%"
C:\Users\H213561\Documents\pscp.exe -l username -pw **** username@mftp.somename.com:/Distribution/somename_corp/*  "Z:\some_name_commercial\%CurrentDate%"

The script is working fine, When I run the above script from command line it does what I intend to do. 该脚本工作正常,当我从命令行运行上述脚本时,它可以完成我打算做的事情。 But I am unable to schedule the script in windows scheduler as a daily job. 但是我无法在Windows Scheduler中安排脚本作为日常工作。

what I tried!!! 我尝试了什么!

I think the problem is with mkdir command, I tried giving a complete path like with no luck. 我认为问题出在mkdir命令上,我尝试给出一个没有运气的完整路径。

"%SystemRoot%\System32\cmd.exe"  /c mkdir  "Z:\some_name_commercial\%CurrentDate%"

PS : Z:\\ is the mounted NAS (network drive) PS:Z:\\是已安装的NAS(网络驱动器)

Update: 更新:

I am able to get this working, when my destination location is not a network drive. 当我的目标位置不是网络驱动器时,我可以使它工作。 But task scheduler doesnt work, if it is a network drive. 但是,如果它是网络驱动器,则任务计划程序将不起作用。

This applies to Windows Vista and later. 这适用于Windows Vista和更高版本。 Vista had a major security update for scheduled tasks. Vista对计划任务进行了重大安全更新。 The purpose is to prevent viruses from spreading though the network when a user is not logged in. 目的是防止未登录用户时病毒通过网络传播。

It's probably a problem with your scheduled task setup. 您计划的任务设置可能存在问题。 Assuming Z: is a mapped network drive, your scheduled task is probably not seeing the mapping. 假设Z:是映射的网络驱动器,则您计划的任务可能看不到映射。 Drive mappings belong to the user so they are only present when the user that created the mapping is logged in. 驱动器映射属于用户,因此它们仅在创建映射的用户登录后才存在。

If you select Run with highest privileges in task setup, then task scheduler uses the built in Administrator account. 如果在任务设置中选择“以最高特权运行”,则任务计划程序将使用内置的管理员帐户。 This account is a separate account with a separate security context. 该帐户是具有单独安全上下文的单独帐户。 This option does not assign higher administrator privileges to the account selected to run the task - it uses the separate account. 此选项不会为选择运行任务的帐户分配更高的管理员特权-它使用单独的帐户。 That built in Administrator account won't have the user's drive mappings. 内置的Administrator帐户将没有用户的驱动器映射。

When running without anyone logged in, task manager uses a different user authentication procedure than what you see as a user. 在没有任何人登录的情况下运行时,任务管理器将使用与您看到的用户不同的用户身份验证过程。 It's called S4U authentication and it denies access to network resources so you won't see mapped drive in that case either. 这称为S4U身份验证,它拒绝访问网络资源,因此在这种情况下您也不会看到映射的驱动器。 And since the networked resources aren't available at all you can't use a UNC either. 而且由于根本无法使用网络资源,因此您也不能使用UNC。 So in that case it can't see the mapped drive at all - not even using the UNC instead of the drive mapping. 因此,在那种情况下,它根本看不到映射的驱动器-甚至不使用UNC而不是驱动器映射。

The only way to get it to run will be to have the user actually logged in when the scheduled task is initiated, Run with highest privileges unchecked, and the task user set to run as the logged in user. 使其运行的唯一方法是在启动计划任务时使用户实际登录,取消选中具有最高特权的运行以及将任务用户设置为以登录用户身份运行。

https://technet.microsoft.com/en-us/library/cc722152(v=ws.11).aspx https://technet.microsoft.com/zh-CN/library/cc722152(v=ws.11).aspx

The scheduled tasks can run in "different sessions" based on your setting. 计划的任务可以根据您的设置在“不同的会话”中运行。

For eg, if you have "Run only when the user is logged on" 例如,如果您具有“仅在用户登录时运行” 在此处输入图片说明

then, you can see that the scheduled tasks would run in the same session as the user. 然后,您可以看到计划的任务将在与用户相同的会话中运行。 The task will NOT run if the user is NOT logged on. 如果用户未登录,该任务将不会运行。 The task will see all the drive mappings of the user 该任务将看到用户的所有驱动器映射

在此处输入图片说明

If setting is "Run whether user is logged on or not", 如果设置为“无论用户是否登录都运行”,

在此处输入图片说明

then, the process runs in a different session, the so called - session 0 然后,该进程将在另一个会话中运行,即所谓的- 会话0

在此处输入图片说明

In this mode, the task can not see the user's drive mappings. 在这种模式下,任务看不到用户的驱动器映射。

(1) Option 1 One way is to explicitly map the drive in your task (1)选项1一种方法是在任务中显式映射驱动器

eg. 例如。

net use Z: /delete
net use Z: <share name>

Then the rest of your task can work with Z: 然后,您的其余任务可以与Z一起使用:

(2) Option 2 (2)选项2

Always use the fully qualified path, ie, \\\\server name\\share name\\folder name 始终使用标准路径,即\\\\server name\\share name\\folder name

In most cases, you would want the task to run irrespective of whether the user is logged in or not. 在大多数情况下,无论用户是否登录,都希望任务运行。 In that case, you should not check this: 在这种情况下,您不应检查以下内容:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过批处理脚本执行参数化的.exe文件(Windows命令行) - Executing parameterized .exe files via batch script (Windows command line) 从带有 2 个条目的 Windows 命令行执行 Perl 脚本 - Executing Perl script from windows-command line with 2 entry Gawk命令未从批处理文件执行,但在命令行上正常 - Gawk command not executing from batch file but fine at command line 在Windows上从批处理脚本执行多个命令 - Executing multiple commands from a batch script on Windows Nodejs Shell脚本在Linux上运行良好,但在Windows中运行不正常。 为什么不执行多个命令 - Nodejs shell script works fine in linux but not in Windows. Why won't it execute more than one command 在 Windows 关机时执行批处理脚本 - Executing a batch script on Windows shutdown 命令行工程-.Bat脚本不起作用 - Command Line Works - .Bat Script Doesn't HTMLHelp编译器可从命令行完美运行,但不能从脚本或批处理文件运行 - HTMLHelp compiler works perfectly from command line but not run from a script or batch file 批处理文件中的Python脚本无法在任务计划程序中运行 - Python script from batch file won't run in task scheduler 在Windows 7命令行上执行ruby脚本,需要多个ruby文件 - Executing a ruby script on Windows 7 command line with multiple ruby files required
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM