“net use” command in a Windows Service

你说的曾经没有我的故事 提交于 2019-12-10 12:33:54

问题


We're using the following command line from within a Windows Service developed with C# .Net Framework 1.1:

net use z: \\myComputer\c$

The service is running under a domain account that is a local administrator on "myComputer". After debugging the code we can see that it does not return any errors but the "z:" drive is never mapped. We've tried the exact same code from a console application and it works properly. What is it that we need to add to the Service to make this work?

The code we're using is included below.

Regards,
Sergio

startInfo.FileName = "net";  
startInfo.Arguments = string.Format(@"use {0}: \\{1}\{2}", driveLetter,
                                    computerName, folder).Trim();  
startInfo.UseShellExecute = false;  
startInfo.RedirectStandardError = true;

proc.EnableRaisingEvents = false;  
proc.StartInfo = startInfo;

proc.Start();

// If there is an error during the mapping of the drive, it will be read
// from the StandardError property which is a StreamReader object and
// be fed into the error output parameter.  
using(StreamReader errorReader = proc.StandardError)  
{  
         string standardError = string.Empty;  
    while((standardError = errorReader.ReadLine()) != null)  
    {  
        error += standardError + " ";  
    }  
}  
proc.WaitForExit();  

回答1:


From http://msdn.microsoft.com/en-us/library/ms685143.aspx:

A service (or any process running in a different security context) that must access a remote resource should use the Universal Naming Convention (UNC) name to access the resource. The service must have appropriate privileges to access the resource. If a server-side service uses an RPC connection, delegation must be enabled on the remote server.

Drive letters are not global to the system. Each logon session receives its own set of drive letters from A to Z. Therefore, redirected drives cannot be shared between processes running under different user accounts. Moreover, a service (or any process running within its own logon session) cannot access the drive letters that were established within a different logon session.

A service should not directly access local or network resources through mapped drive letters, nor should it call the net use command to map drive letters at run time.




回答2:


You cannot access user properties from a windows service (including the HKEY-CURRENT-USER from the registry) because the service does not run as a logged in user.

Mapped drives are part of user settings, so you cannot use them as a service unless you dig through to find the user properties in the registry manually, map the drives in your service, etc. It's a pain.

What you may want to try and do is ask a question about how to have your Service execute the login sequence (probably some .EXE). That may do it for you.

Hope this helps, Alan.




回答3:


You probably need to specify the account used for the login. Type net use /? on a command prompt to get help setting that up with the command.




回答4:


I suspect that it is because the service is not running in the context of the local user. As I remember, you can configure a windows service from years-ago to 'interact with the desktop' or something similar.




回答5:


I was doing something similar to log in to a remote server, but without the mapped drive part. I don't like using mapped drives; in programs that is, I use subst for convenience all the time. Anyway, I just had to make sure to include

use \\server\c$ /user:admin password

or whatever your user/password is that has access to the remote server, then it doesn't matter what the service is logged on as.



来源:https://stackoverflow.com/questions/281951/net-use-command-in-a-windows-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!