Map network drive for the SYSTEM user in Windows
Simple solution through cmd (Administrator privileges) 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 suggests the following 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
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 **
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:
- Create a batch file that mounts your drive (include the
net use
command that you were already using). - 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.