Map a network space to a Windows Drive in a scheduled job (script)?

I need to map to a network space a mapped drive such as S: for some other jobs. My box is Windows Server 2008. In Windows Server 2008, I can create a scheduled task with a bat job, and I can specify it runs when Windows is reboot or Log on without specified user. It like a service with local system. The bat contains net cmd to map the drive:

net use S: \\netdrive1\space1 pwd /user:oneUser


Then I reboot the box. It looks like that the scheduled job did run. I can check from the Task Scheduler job's history. However, the S: drive cannot be mapped. If I just run the bat from the command line. It works fine. It seems that I have to log on the box to make the mapped drive. Not sure if it is possible to let Windows to run the job as local system when it reboots to make the drive available?

For my case, the network drive is at a Unix box.

As I mentioned above, I have some other dependency scheduled jobs under Windows local system. Therefore, I have to make the network space available without any user log on.

-
why mapped drives, can't unc paths be used? –  tony roth Sep 1 '10 at 20:52
The drive access need to pass user name and password for authentication. –  David.Chu.ca Sep 1 '10 at 21:29

Mapped drives are only available to an interactive session. You can't map a drive while running as a service, you need to use UNC paths.

-

You're stuck in a bit of a quandary... the scheduler is executing your bat file in "administrator mode" which doesn't really affect the user-mode environment. I know that sounds odd, but my own testing shows that it might as well be 2 separate user environments. You might want to try running a quick & dirty vbs script in the user profile's startup folder... and see if that does what you want.

set WshShell = WScript.CreateObject("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")
do while true
objNetwork.MapNetworkDrive "S:", "\\netdrive1\space1", false, "oneUser", "pwd"
WScript.Sleep(12000)
loop

If the drive doesn't get unmapped... this will throw an error the next loop around when it tries to re-map the drive... unless you catch the error... or do a quick & dirty

On Error Resume Next
before the loop.

You can adjust the sleep time as needed... or do some more advanced time/date checking mechanism... but this should give you the idea.

-
That'll still require a user logon which I think the OP is trying to avoid? –  chunkyb2002 Sep 1 '10 at 21:22
If there is an interactive session... someone had to log on at some point... non-interactive users (running as a service or via the scheduler) can mount their own drive letters without having to turn to a scheduler to re-map a volume. –  TheCompWiz Sep 3 '10 at 18:41

net use S: \\netdrive1\space1 pwd /user:oneUser /persistent:yes

That might do away with the need for the scheduled task but I'm not sure if it will maintain the connection without a user logon.

UNC paths in your application is a much better way than mapping the drive so if possible you should do that instead.

http://technet.microsoft.com/en-us/library/bb490717.aspx

EDIT

This is not very elegant but it might work (completely untested)

NET USE \\netdrive1\space1 /USER:oneUser pwd && PUSHD \\netdrive1\space1
COPY C:\path\to\files\*.dat Z:\
POPD && NET USE \\netdrive1\space1 /DELETE


Try that as your batch file. What's happening is NET USE authenticates the user against the share, PUSHD automatically mounts the drive to a drive letter (in reverse order so assuming no other drives are mounted, which there isn't in this case, it will be Z:). Copy the files then kill the connection to the share. I have no idea if this will work without interactive login session but it's worth a shot.

-
If I use UNC like this: copy *.dat \\netDrive1\space1, I got a failure message saying "Log on failure: unknown user name or password". Not sure if I should do anything to \\netDrive1 with user/pwd first? –  David.Chu.ca Sep 1 '10 at 21:20
See my edit above, might not be much use as I'm fairly sure Zypher is correct and mapped drives aren't available without an interactive session. –  chunkyb2002 Sep 2 '10 at 20:19
I tried but still no working. Thanks anyway. It is interesting to see the usage of NET USE without drive name. The PUSHD will assign a drive to the network drive like Z: in your codes but X: in my box since Z: and Y: are used. Anyway, I think it might be problem to schedule a job to map to network drive. I event tried to make the drive first and then tried to "IF EXIST S:*.* echo S: is there" as a local system scheduled job. I run the job, still the job does not see the drive. –  David.Chu.ca Sep 3 '10 at 21:03

Have you considered using a UNC path instead of trying to map a drive letter? In 98% of cases using \\server\share\ wherever you spot a s: causes no problems.

-

I was able to run my scheduled task as my desired user, and just use NET USE Q: \\192.168.7.1\space as the first line of the batch script that was run.

Unfortunately, I didn't have an option to use UNC paths directly, due to the way third party libraries were treating the UNC paths.

-