假设一些Windows服务使用的代码需要映射网络驱动器,而不需要UNC路径。当服务启动时,如何使驱动器映射可用于服务的会话?作为服务用户登录并创建持久映射将不会在实际服务的上下文中建立映射。


当前回答

我还不能评论(在声誉方面),但创建了一个帐户,只是为了回答@Tech Jerk @spankmaster79(好名字lol)和@NMC问题,他们在回复“我发现了一个解决方案,与psexec类似,但不需要额外的工具,并在重启后幸存。”

解决方案是在登录帐户中浏览到该文件夹,即:

    \\servername\share  

并让它提示登录,并输入您在psexec中用于UNC的相同凭据。之后就开始工作了。在我的例子中,我认为这是因为提供服务的服务器与我要映射到的服务器不属于同一个域。我在想,如果UNC和计划任务都是指IP而不是主机名

    \\123.456.789.012\share 

它可能会完全避免这个问题。

如果我曾经得到足够的代表点在这里,我会把这作为一个回复。

其他回答

你可以使用'net use'命令:

var p = System.Diagnostics.Process.Start("net.exe", "use K: \\\\Server\\path");
var isCompleted = p.WaitForExit(5000);

如果这在服务中不起作用,请尝试Winapi和PInvoke WNetAddConnection2

编辑:显然我误解你了——你不能改变服务的源代码,对吗?在这种情况下,我会遵循mdb的建议,但稍微改变一下:创建自己的服务(让我们称之为映射服务)来映射驱动器,并将这个映射服务添加到第一个(实际工作的)服务的依赖项中。这样,在映射服务启动(并映射驱动器)之前,工作服务将不会启动。

The reason why you are able to access the drive in when you normally run the executable from command prompt is that when u are executing it as normal exe you are running that application in the User account from which you have logged on . And that user has the privileges to access the network. But , when you install the executable as a service , by default if you see in the task manage it runs under 'SYSTEM' account . And you might be knowing that the 'SYSTEM' doesn't have rights to access network resources.

这个问题有两种解决方案。

To map the drive as persistent as already pointed above. There is one more approach that can be followed. If you open the service manager by typing in the 'services.msc'you can go to your service and in the properties of your service there is a logOn tab where you can specify the account as any other account than 'System' you can either start service from your own logged on user account or through 'Network Service'. When you do this .. the service can access any network component and drive even if they are not persistent also. To achieve this programmatically you can look into 'CreateService' function at http://msdn.microsoft.com/en-us/library/ms682450(v=vs.85).aspx and can set the parameter 'lpServiceStartName ' to 'NT AUTHORITY\NetworkService'. This will start your service under 'Network Service' account and then you are done. You can also try by making the service as interactive by specifying SERVICE_INTERACTIVE_PROCESS in the servicetype parameter flag of your CreateService() function but this will be limited only till XP as Vista and 7 donot support this feature.

希望这些解决方案对你有帮助。如果这对你有用,请告诉我。

我发现了一个非常简单的方法:使用powershell的“New-SmbGlobalMapping”命令,它将全局挂载驱动器:

$User = "usernmae"
$PWord = ConvertTo-SecureString -String "password" -AsPlainText -Force
$creds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord
New-SmbGlobalMapping -RemotePath \\192.168.88.11\shares -Credential $creds -LocalPath S:

我还不能评论(在声誉方面),但创建了一个帐户,只是为了回答@Tech Jerk @spankmaster79(好名字lol)和@NMC问题,他们在回复“我发现了一个解决方案,与psexec类似,但不需要额外的工具,并在重启后幸存。”

解决方案是在登录帐户中浏览到该文件夹,即:

    \\servername\share  

并让它提示登录,并输入您在psexec中用于UNC的相同凭据。之后就开始工作了。在我的例子中,我认为这是因为提供服务的服务器与我要映射到的服务器不属于同一个域。我在想,如果UNC和计划任务都是指IP而不是主机名

    \\123.456.789.012\share 

它可能会完全避免这个问题。

如果我曾经得到足够的代表点在这里,我会把这作为一个回复。

而不是依赖于一个持久驱动器,你可以设置脚本映射/取消映射驱动器每次你使用它:

net use Q: \\share.domain.com\share 
forfiles /p Q:\myfolder /s /m *.txt /d -0 /c "cmd /c del @path"
net use Q: /delete

这对我很有用。