Search code examples
batch-filescheduled-taskswindows-task-schedulernetwork-drivedrive-mapping

How to refer/ map network drive during Task Scheduler/ bat job without user logged in/ mapping?


I have a network drive mapped, let's call it the K: drive. I can also see this drive is \marketing.fakename.blah.com, for example.

I'm trying to run a .bat file and scripts on a remote server via Windows Task Scheduler (regardless if I'm logged on, run with highest privileges).

Anyway with the scripts I'm getting an error "can't find K: drive".

Now when I'm logged in on the server, I do have the drive mapped and having "K:\blahblahfiles" being searched for works just fine.

I'm sadly not the expert with task scheduler and .bat files, but it seems it does run in the background, sort of. Well, let's say the .bat files opens the command line, but when the Task Scheduler runs the .bat, I don't see the command line, even though I have "use my user account" under Security options.

I'm not exactly sure what to do here.

Is there another way to specify the K:\ drive to make it more universal? Should I simply try something like \marketing.fakename.blah.com\resources\filename.whatever? I tried that and it didn't quite work out.

Should I amend the original .bat file to map the K drive somehow immediately before the rest of the script is run?


Solution

  • To access a network path in windows, use:

    \\host\directory\file
    

    As per net help names in CMD:

    Network path

    A description of the location of a shared resource, consisting of a computer's computername followed by the sharename of the resource. The computername is preceded by two backslashes, and the sharename is preceded by one backslash (for example, \SERVER1\RESOURCE).

    To map the drive K: to a network path, put this near the top of your batch file:

    net use K: \\host\directory