Solved

Using a network drive from a Windows Service

Posted on 2004-03-22
19
1,054 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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
 
LVL 3

Author Comment

by:jetaasen
ID: 10660820
0
 

Accepted Solution

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

Featured Post

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!

Question has a verified solution.

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

by Batuhan Cetin In this article I will be guiding through the process of removing a failed DC metadata from Active Directory (hereafter, AD) using the ntdsutil tool in a Windows Server 2003 environment. These steps are not necessary in a Win…
Recently, I had the need to build a standalone system to run a point-of-sale system. I’m running this on a low-voltage Atom processor, so I wanted a light-weight operating system, but still needed Windows. I chose to use Microsoft Windows Server 200…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

735 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