Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Using a network drive from a Windows Service

Posted on 2004-03-22
19
Medium Priority
?
1,077 Views
Last Modified: 2010-05-18
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
Comment
Question by:jetaasen
  • 7
  • 6
  • 2
  • +2
19 Comments
 
LVL 16

Expert Comment

by:JamesDS
ID: 10647584
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
 
LVL 3

Author Comment

by:jetaasen
ID: 10647669
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
 
LVL 16

Expert Comment

by:JamesDS
ID: 10647698
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:jetaasen
ID: 10647849
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
 
LVL 16

Expert Comment

by:JamesDS
ID: 10647960
in which case, you got me
Sorry, no idea

JamesDS
0
 
LVL 8

Expert Comment

by:Emptyone
ID: 10650301
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
 
LVL 2

Expert Comment

by:Ugrum
ID: 10650506
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
 
LVL 16

Expert Comment

by:JamesDS
ID: 10650793
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
 
LVL 3

Author Comment

by:jetaasen
ID: 10651701
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
 
LVL 16

Expert Comment

by:JamesDS
ID: 10655713
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
 
LVL 3

Author Comment

by:jetaasen
ID: 10656291
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
 
LVL 8

Expert Comment

by:Emptyone
ID: 10656329
That was almost what I suggested in the middle of this tread.
0
 
LVL 3

Author Comment

by:jetaasen
ID: 10656423
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
 
LVL 2

Expert Comment

by:Ugrum
ID: 10656841
Will you try localsystem mapping from command line?
0
 
LVL 3

Author Comment

by:jetaasen
ID: 10657012
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
 
LVL 16

Expert Comment

by:JamesDS
ID: 10659719
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
 

Accepted Solution

by:
PashaMod earned 0 total points
ID: 10694371
Closed, 500 points refunded.
PashaMod
Community Support Moderator
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Organizations create, modify, and maintain huge amounts of data to help their businesses earn money and generally function.  Typically every network user within an organization has a bit of disk space to store in process items and personal files.   …
This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
Integration Management Part 2
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question