Task Scheduler - Access non-local drives while running task not logged in

AMcNall picture AMcNall · Jun 26, 2014 · Viewed 30k times · Source

I had a task in win scheduler that runs every minute. The task runs a .bat file which SVN updates a series of folders then executes a perl script (which in turn will run off several others). The output/'log' of the perl script (tagged by date/time) is sent to a shared drive, not local to the machine the task runs on.

The whole task works flawlessly until sending the log to the shared drive. Since the task needs to run every minute I figured it would be best to use the setting 'Run whether user is logged on or not' in task scheduler.. A box beneath is detailed 'Do not store password. The task will only have access to local computer resources.' Obviously this is not going to work for me, so I left it unchecked.

However the script does still not write to the shared drive, even when logged in. Does anyone have any suggestions on how to work around this? I also realised I could 'Run only when the user is logged in' (this DOES write to shared drive), but in that case I would need a definite way to ensure the account never logs off, and cannot be force logged off by other users (the computer is shared by a number of users, max two at a time).

Thoughts?

Answer

Mofi picture Mofi · Jun 26, 2014

phd443322 has given already the right answer.

Mappings of a shared folders to drive letters are saved per user account in Windows registry if the network connection was made with default settings. Therefore every user using a computer can have different network drives.

There is the option Reconnect at logon to map the shared folder to a drive letter which is by default checked. But this option can be unchecked before creating the connection to map the shared folder to a drive letter just for current user session.

net use /? entered in a command prompt window outputs help of this command which is used to map a shared folder to a drive letter from command line or a batch file. There is the option /PERSISTENT:{YES | NO} whereby the default is YES. For example

net use Z: \\computer\share /persistent:no

connects a shared folder as drive Z: for current user session only.

This explains why AMcNall's failed with using a network drive in automated task.

Extra hint:

On using a laptop which is used in the office connected to company network, but also at home in a private network or no network, and finally also often in other networks, it is advisable to avoid an automatic network drive connection by Windows after logon in my point of view. It is better to use a batch file located somewhere on hard disk with a shortcut to this batch file stored in folder Startup of the user's Windows start menu with the property set to run the batch file with minimized window. The batch file contains for example:

@echo off
echo Checking availability of server/computer XXX
%SystemRoot%\system32\ping.exe -n 1 XXX >nul
if errorlevel 1 goto EndBatch
echo Map shared folder XXX\share to drive letter Z:
%SystemRoot%\system32\net.exe use Z: \\XXX\share /persistent:no
:EndBatch