简体   繁体   中英

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

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

what I tried!!!

I think the problem is with mkdir command, I tried giving a complete path like with no luck.

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

PS : Z:\\ is the mounted NAS (network drive)

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. Vista had a major security update for scheduled tasks. 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. 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.

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. And since the networked resources aren't available at all you can't use a UNC either. So in that case it can't see the mapped drive at all - not even using the UNC instead of the drive mapping.

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

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

在此处输入图片说明

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

eg.

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

Then the rest of your task can work with Z:

(2) Option 2

Always use the fully qualified path, ie, \\\\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:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM