Map a network drive to be used by a service

前端 未结 12 1991
再見小時候
再見小時候 2020-11-22 10:43

Suppose some Windows service uses code that wants mapped network drives and no UNC paths. How can I make the drive mapping available to the service\'s session when the servi

相关标签:
12条回答
  • 2020-11-22 11:36

    Instead of relying on a persistent drive, you could set the script to map/unmap the drive each time you use it:

    net use Q: \\share.domain.com\share 
    forfiles /p Q:\myfolder /s /m *.txt /d -0 /c "cmd /c del @path"
    net use Q: /delete
    

    This works for me.

    0 讨论(0)
  • 2020-11-22 11:36

    I can't comment yet (working on reputation) but created an account just to answer @Tech Jerk @spankmaster79 (nice name lol) and @NMC issues they reported in reply to the "I found a solution that is similar to the one with psexec but works without additional tools and survives a reboot." post @Larry had made.

    The solution to this is to just browse to that folder from within the logged in account, ie:

        \\servername\share  
    

    and let it prompt to login, and enter the same credentials you used for the UNC in psexec. After that it starts working. In my case, I think this is because the server with the service isn't a member of the same domain as the server I'm mapping to. I'm thinking if the UNC and the scheduled task both refer to the IP instead of hostname

        \\123.456.789.012\share 
    

    it may avoid the problem altogether.

    If I ever get enough rep points on here i'll add this as a reply instead.

    0 讨论(0)
  • 2020-11-22 11:39

    I found a solution that is similar to the one with psexec but works without additional tools and survives a reboot.

    Just add a sheduled task, insert "system" in the "run as" field and point the task to a batch file with the simple command

    net use z: \servername\sharedfolder /persistent:yes
    

    Then select "run at system startup" (or similar, I do not have an English version) and you are done.

    0 讨论(0)
  • 2020-11-22 11:43

    Found a way to grant Windows Service access to Network Drive.

    Take Windows Server 2012 with NFS Disk for example:

    Step 1: Write a Batch File to Mount.

    Write a batch file, ex: C:\mount_nfs.bat

    echo %time% >> c:\mount_nfs_log.txt
    net use Z: \\{your ip}\{netdisk folder}\ >> C:\mount_nfs_log.txt 2>&1
    

    Step 2: Mount Disk as NT AUTHORITY/SYSTEM.

    Open "Task Scheduler", create a new task:

    1. Run as "SYSTEM", at "System Startup".
    2. Create action: Run "C:\mount_nfs.bat".

    After these two simple steps, my Windows ActiveMQ Service run under "Local System" priviledge, perform perfectly without login.

    0 讨论(0)
  • 2020-11-22 11:46

    Use this at your own risk. (I have tested it on XP and Server 2008 x64 R2)

    For this hack you will need SysinternalsSuite by Mark Russinovich:

    Step one: Open an elevated cmd.exe prompt (Run as administrator)

    Step two: Elevate again to root using PSExec.exe: Navigate to the folder containing SysinternalsSuite and execute the following command psexec -i -s cmd.exe you are now inside of a prompt that is nt authority\system and you can prove this by typing whoami. The -i is needed because drive mappings need to interact with the user

    Step Three: Create the persistent mapped drive as the SYSTEM account with the following command net use z: \\servername\sharedfolder /persistent:yes

    It's that easy!

    WARNING: You can only remove this mapping the same way you created it, from the SYSTEM account. If you need to remove it, follow steps 1 and 2 but change the command on step 3 to net use z: /delete.

    NOTE: The newly created mapped drive will now appear for ALL users of this system but they will see it displayed as "Disconnected Network Drive (Z:)". Do not let the name fool you. It may claim to be disconnected but it will work for everyone. That's how you can tell this hack is not supported by M$.

    0 讨论(0)
  • 2020-11-22 11:46

    The reason why you are able to access the drive in when you normally run the executable from command prompt is that when u are executing it as normal exe you are running that application in the User account from which you have logged on . And that user has the privileges to access the network. But , when you install the executable as a service , by default if you see in the task manage it runs under 'SYSTEM' account . And you might be knowing that the 'SYSTEM' doesn't have rights to access network resources.

    There can be two solutions to this problem.

    1. To map the drive as persistent as already pointed above.

    2. There is one more approach that can be followed. If you open the service manager by typing in the 'services.msc'you can go to your service and in the properties of your service there is a logOn tab where you can specify the account as any other account than 'System' you can either start service from your own logged on user account or through 'Network Service'. When you do this .. the service can access any network component and drive even if they are not persistent also. To achieve this programmatically you can look into 'CreateService' function at http://msdn.microsoft.com/en-us/library/ms682450(v=vs.85).aspx and can set the parameter 'lpServiceStartName ' to 'NT AUTHORITY\NetworkService'. This will start your service under 'Network Service' account and then you are done.

    3. You can also try by making the service as interactive by specifying SERVICE_INTERACTIVE_PROCESS in the servicetype parameter flag of your CreateService() function but this will be limited only till XP as Vista and 7 donot support this feature.

    Hope the solutions help you.. Let me know if this worked for you .

    0 讨论(0)
提交回复
热议问题