views:

4089

answers:

7

I'm trying to map network drive from windows service, I use batch file for executing the following command

NET USE U: \\192.168.55.6\folder password

While executing batch file either in service constructor or in onstart event, drive is not mapped?

        Process process = new Process();
        process.StartInfo.FileName = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\MAP.BAT";
        process.StartInfo.CreateNoWindow = false;
        process.Start();

How does one map network drive from windows service?

A: 

I think services can't map network drives unless they have the right "interact with desktop" ?

Try to run the service under the "Network Service" account, instead of "Local System" ?

Andomar
This also doesn't work.
Ahmed
Can you log the output of the map command? like: "net use ... > c:\map.log"
Andomar
This shouldn't matter I think - network mappings are not managed by the shell. They're kernel-level links. See WinObj from SysInternals.
MSalters
My understanding is that the user and the service use the same list of mappings, so that changes from the service could impact the user. Hence the requirement for "interact with desktop". My knowledge here is a bit dated tho (> 8 years :D)
Andomar
@Andomar: no errors occured while running batch.
Ahmed
Would be severly different nowadays. Drive letters are associated with the user session; the desktop is associated with a "WinStation".
MSalters
Any other suggestions rather than pinvoke?
Ahmed
How do you notice the drive is not mapped? If what MSalters says is right, you won't see a drive mapped by a service in explorer. Because they're in a different user session.
Andomar
A: 

Are you running the service under the user account that belongs to the password? The MAP USE command will use the current user, unless you pass /USER:anotheruser

MSalters
Yes. And this user account has permissions on that shared folder.
Ahmed
A: 

It might be better to just call the right API function, instead of calling a batch file to call another executable. The function you're looking for is DefineDosDevice()

MSalters
A: 

As far as I know, a mapped drive is only mapped for the duration of the user session. If your windows service is running on startup, or is otherwise not part of a user session, the drive mapping is lost.

This may or may not be true but it is worth looking into - I remember something about it from a similar scenario I had about 7 years ago.

Winston Smith
A: 

There are two issues.

1) Mappings are only in use for the user session, which means that effectively you can't used a mapped drive for a Service. You would need to use the UNC path.

2) The second issue is that a service (using the local system account) does not have access to the network, or more specifically, to the resource required. To resolve this, you would need to, either: Give the 'computer' on which the service is running specific access to the folder, or, set up the service to use a network (DOMAIN) account that has access to the resource.

David L Morris
A: 

All issues solved by using Map Network Drive (API) to map network drive. I map required drives while OnStart event of service.

Ahmed
A: 

I tried to map the drive OnStart method of the windows service with the specified class but ... no success. Can you help me?

Alex