7

As a rule of thumb Windows services can't access mapped drives and you have to use UNC paths instead.

I'm convinced that there's a way around this so that a Windows service can access a mapped drive but I can't find it anywhere.

My specific case is that I'm running Tomcat7 on a Windows 2008 server.

Edd
  • 399
  • 1
  • 3
  • 12

3 Answers3

16

An alternative to using mapped directories or UNC paths is to use symlinks.

NTFS symbolic links (symlinks) can refer to a UNC path but differ from shortcuts in that there is no redirect to the requested location. If you create a symlink as the following...

mklink /D C:\myLink \\127.0.0.1\c$

... then when you open C:\myLink the address of the folder you are in will be C:\myLink and not \\127.0.0.1\c$, which is what you would get if myLink was a shortcut and not a symlink. This is significant if your application has compatibility issues with UNC paths.

Additionally the symlink exists in the filesystem and does not need to be recreated after logon as your average mapped drive requires (generally automated) and is therefore available to Windows services.

Edd
  • 399
  • 1
  • 3
  • 12
  • 3
    Little info from my experience, the command only works in Windows Command Prompt and not in Power Shell – Muffun Jun 08 '16 at 15:52
  • That's wild.... and very useful. – Marki Apr 30 '19 at 14:06
  • 1
    With this I get "The user name or password is incorrect." on all account except on the one I already connected before to the same share and entered the password. How is this supposed to work with a service. With a "open access for all" share? – David Balažic Dec 06 '20 at 15:15
3

I've found a solution to this problem that seems to be working nicely: https://stackoverflow.com/a/7867064/669645

Steps that I took:

  1. Create a bat file which contains the command net use z: \servername\sharedfolder /persistent:yes
  2. Create a scheduled task
    • Set user as "System"
    • Add an action to run the bat file
  3. Manually run the task (no need to set a schedule)

Note: the drive will appear as "Disconnected Network Drive (Z:)" but will still be accessible to all logged in users and also windows services

Edd
  • 399
  • 1
  • 3
  • 12
  • This isn't actually working for me as the mapped drive isn't persisting after a reboot (fine on Windows 7 but not Windows Server 2008). If I schedule the task to run on startup I still can't see the mapped drive – Edd Sep 25 '13 at 16:44
1

Just as an addendum to the excellent answers already provided, and as an answer to a question about using powershell, above, I would add that the Powershell equivalent of the accepted answer:

mklink /D C:\myLink \\127.0.0.1\c$

is this:

New-Item -Path C:\myLink -ItemType SymbolicLink -Target \\127.0.0.1\c$
user218392
  • 11
  • 2
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 03 '22 at 16:14
  • I just don't see what the issue is with this answer. This references the symbolic link answer and provides useful information regarding a comment on that answer. This answer makes the accepted answer more complete. if this was combined with the accepted answer like an addendum - it is perfect. – StixO Jan 05 '23 at 12:16