Solved

Using a network drive from a Windows Service

Posted on 2004-03-22
19
1,045 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
 
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
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

760 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now