Map network drive for the SYSTEM user in Windows

7

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.

Javier Mr

Posted 2014-05-14T14:32:31.627

Reputation: 259

Answers

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

was

Posted 2014-05-14T14:32:31.627

Reputation: 81

Wow, just running the scheduled task affects the service if it's restarted. Awesome. – eel ghEEz – 2019-05-01T19:44:38.920

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.

bdr9

Posted 2014-05-14T14:32:31.627

Reputation: 2 508

2

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 **

Phil B

Posted 2014-05-14T14:32:31.627

Reputation: 21