8

I have a piece of software which needs access to files as if they are local to the machine, so I just mapped the network drive as a letter using net use. This works as expected, however the software itself runs as a service with the SYSTEM user account, this is not optional nor configurable. I currently have the drive mapped in the user from which I executed the net use.

So my question is, how do I map a drive so the SYSTEM account has access to it?

As pointed out in this question there is no way for mapping a drive for all users.

An in more general case how do you mount a remote location into the local file system for the whole system (at boot time is perfectly ok), in the same fashion as is done in Linux.

7

Simple solution through cmd (with admin privs) without any batch files would be:

schtasks /create /tn "my_mount" /tr "net use V: \\hostname\directory /persistent:yes" /sc onstart /RU SYSTEM

followed by a restart of your server or by executing:

schtasks /run /tn "my_mount"

EDIT: user alfa_989 sutgests folloving command extension, which I haven't tested. But it still might be helpful for somebody:

schtasks /create /tn "my_mount" /tr "net use V: \hostname\directory /persistent:yes" /sc onstart /RU SYSTEM /RL HIGHEST

  • Wow, just running the scheduled task affects the service if it's restarted. Awesome. – eel ghEEz May 1 '19 at 19:44
3

I had the same problem where a Bulk Copy Job (BCP) on SQL Server needed to write a file out to a different server, managed by a different group in the organization.

The trick is to get the drive mapped as the user that the BCP job is using. You map the drive as a dedicated active directory account that has permissions for the desired destination, what some call a service account. I had to work with my AD folks to get one setup. To find out what user account is being used by BCP run this command from a SQL Server Query window:

EXEC xp_cmdshell 'ECHO %USERDOMAIN%\%USERNAME%

When mapping the drive, use whoami at the command prompt.

In my case, I was unable to be logged into the SQL Server as the SYSTEM account, so I built a batch job that could then be executed by Task Scheduler, but run AS the SYSTEM account. I put commands in the batch job to write the results out to a text file, since I wouldn't be able to see it.

** Batch job below **

ECHO ON

ECHO This will map the drive, but is being run by task scheduler AS the user SYSTEM

ECHO which should make it accessible to the user SYSTEM

ECHO List the existing drives first.

net use >> c:\Test\SystemNetUseOutput.txt

net use R: \\MyRemoteServer\MyDirectory\ /P:Yes /u:MyDomain\MyUsername pa$$word

ECHO the /P switch makes the drive remain after reboot

ECHO List the existing drives with the new mapping

net use >> c:\Test\SystemNetUseOutput.txt

ECHO See what user this batch job ran under

whoami >> c:\Test\SystemNetUseOutput.txt

ECHO need to exit to allow the job to finish

EXIT

** I hope this helps someone else **

2

I have encountered this issue when trying to use CrashPlan to backup a network share. Because CrashPlan's service runs as a SYSTEM user, it cannot access network shares that were mapped from user accounts. CrashPlan provides a workaround on Windows computers.
The workaround solved the problem for me and allowed CrashPlan's service to access my network shares. This should also pertain to your issue because it has the same root cause.

CrashPlan's article on this issue:
http://support.code42.com/../Mounting_Networked_Storage_Or_NAS_Devices_For_Backup

CrashPlan's workaround for Windows:
http://support.code42.com/CrashPlan/Latest/Backup/Back_Up_a_Windows_Network_Drive

Summary:

  1. Create a batch file that mounts your drive (include the net use command that you were already using).
  2. Use Task Scheduler to create a task that automatically runs this batch file on startup. The task can be set to run as the SYSTEM user.
  • Extremely late answer, but I'd say this is a pretty inelegant solution that can result in a race condition if an application attempts to access data before the mount has taken place – Manchineel 2 days ago

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.