• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1082
  • Last Modified:

Using a network drive from a Windows Service

Is it possible to map a network drive and then have it available to a service? The service does not support UNC paths and it can't map the drive itself.

I have tried to log in, map the drive and then start the service as the logged-in user. But the drive is not available to the service.

Of course I want the service to start automatically at system startup, but if I can get the above to work, that can probably be solved by mapping and starting from a batch file that runs as a scheduled task on system startup.
0
jetaasen
Asked:
jetaasen
  • 7
  • 6
  • 2
  • +2
1 Solution
 
JamesDSCommented:
jetaasen

You can use SRVANY.EXE from the resource kit to set a batch file to start as a service, you could use that to create your drive mappaing

Resource kit tools and lots of useful info are available from www.microsoft.com/reskit

Hope this helps

Cheers

JamesDS
0
 
jetaasenAuthor Commented:
Hi James!

I am familiar with SRVANY, but I don't think that will help in my case. I know how to create a drive mapping at startup, but my problem is that I want to make a drive mapping available to an existing service. And I can't change the actual service.

I have a batch file that maps a drive and starts the service, but the service does not see the mapped drive even though it runs as the same user that mapped the drive. I even tried to start that batch file as a service (using srvany), but that did not change anything.
0
 
JamesDSCommented:
jetaasen

The only other thing I can think of would be making use of the SUBST command in some way. Failing that I don't think there will be another solution short of copying the files off the share locally.

Cheers

JamesDS
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
jetaasenAuthor Commented:
Actually I am using the SUBST command when mapping my drive. But as far as I can see there's no difference between SUBST and NET USE in this case (I have of course tried both).
0
 
JamesDSCommented:
in which case, you got me
Sorry, no idea

JamesDS
0
 
EmptyoneCommented:
Use SRVANY to get the drive mapped by a batch file. Then set the service you want to use this mapping dependent of the one you made. Then it will map the drive before the service starts
0
 
UgrumCommented:
In order to make the mapped network drive available to the service, you need to map it using the account that is configured for that service. In other words, log in with that account, map the drive (make sure to select "reconnect at logon"), and you're done.
In case of service running at SYSTEM account, you may map the drive using the localsystem console, which can be obtained by "at" trick. From the command line run "at 12:10 /interactive cmd.exe" - make sure to specify the time one or two minutes ahead of the current time. When the scheduled command run, you will get a console running at the local system credentials, use it to map the drive.
0
 
JamesDSCommented:
Ugrum

Seems like a good idea, but correct me if i'm wrong drives are only returned using this method at interactive logon and since the service is not performing an interactive logon this can't work.

Jetaasen - please test it and let us know - I look forward to being proved wrong.

Cheers
JamesDS
0
 
jetaasenAuthor Commented:
You are right, James. As I stated in my original question, my first try was to log in, map the drive and then start the service as the same user. But the drive is still not available to the service.

I haven't actually tried the thing with SYSTEM account, but I don't think that will work anyway since I need a domain user to map to the network drive.
0
 
JamesDSCommented:
jetaasen

Shame - a solution would have been nice

I confess I'm beaten and can't think of anything else to help you
Sorry
Cheers

JamesDS
0
 
jetaasenAuthor Commented:
I have found a way to do this now. It will work if I create another service that maps the drive and this service must run as the same account that the application service runs as. For me it worked fine using LocalSystem.

1) Create a new service that will map the drive (ie. MapDriveService) with the following in OnStart (VB.NET sample):
  Protected Overrides Sub OnStart(ByVal args() As String)
    Try
      Dim netObj As Object = CreateObject("WScript.Network")
      netObj.MapNetworkDrive("Q:", "\\computer\share", "False", "DOMAIN\user", "password")
    Catch ex As Exception
      EventLog.WriteEntry(ex.ToString(), EventLogEntryType.Warning)
    End Try
  End Sub

2) Install the service and set the startup type to Manual.

3) Make the Application service dependent on the mapping service

4) Whenever the Application Service starts, it will first start the mapping service and it will then have the network drive available.

0
 
EmptyoneCommented:
That was almost what I suggested in the middle of this tread.
0
 
jetaasenAuthor Commented:
JamesDS suggested SRVANY before you, but anyway the SRVANY solution did not work. And as I stated in my original question, I had already tried with a batch file that mapped the drive and then started the service and that did not work. I can't see why it would make any difference introducing SRVANY or dependencies.

What made the difference was to create a native service that run with the same account as my application service.
0
 
UgrumCommented:
Will you try localsystem mapping from command line?
0
 
jetaasenAuthor Commented:
Again, the difference was to have a native service do the mapping. The drive is not available if the mapping is done from a command prompt or a batch file started from SRVANY, at or whatever.

I'm not sure how to close this question, so I have posted a request to community support. I hope they will award you with points if they think I'm wrong in not accepting any of your comments. Thank you all for your efforts.
0
 
JamesDSCommented:
jetaasen

Thank you for the recommendation that I should get some points.
Sorry I couldn't fix it, but it was a pleasure to try

Cheers

JamesDS
0
 
jetaasenAuthor Commented:
0
 
PashaModCommented:
Closed, 500 points refunded.
PashaMod
Community Support Moderator
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 6
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now