Solved

Using a network drive from a Windows Service

Posted on 2004-03-22
19
1,065 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

I've always wanted to allow a user to have a printer no matter where they login. The steps below will show you how to achieve just that. In this Article I'll show how to deploy printers automatically with group policy and then using security fil…
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.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

623 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