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


当前回答

您要么需要修改服务,要么将其包装到辅助进程中:除了会话/驱动器访问问题外,持久驱动器映射仅在交互式登录时恢复,而服务通常不会执行。

helper进程方法可以非常简单:只需创建一个映射驱动器的新服务并启动“真正的”服务。唯一不是完全无关紧要的事情是:

The helper service will need to pass on all appropriate SCM commands (start/stop, etc.) to the real service. If the real service accepts custom SCM commands, remember to pass those on as well (I don't expect a service that considers UNC paths exotic to use such commands, though...) Things may get a bit tricky credential-wise. If the real service runs under a normal user account, you can run the helper service under that account as well, and all should be OK as long as the account has appropriate access to the network share. If the real service will only work when run as LOCALSYSTEM or somesuch, things get more interesting, as it either won't be able to 'see' the network drive at all, or require some credential juggling to get things to work.

其他回答

我发现了一个非常简单的方法:使用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:

您要么需要修改服务,要么将其包装到辅助进程中:除了会话/驱动器访问问题外,持久驱动器映射仅在交互式登录时恢复,而服务通常不会执行。

helper进程方法可以非常简单:只需创建一个映射驱动器的新服务并启动“真正的”服务。唯一不是完全无关紧要的事情是:

The helper service will need to pass on all appropriate SCM commands (start/stop, etc.) to the real service. If the real service accepts custom SCM commands, remember to pass those on as well (I don't expect a service that considers UNC paths exotic to use such commands, though...) Things may get a bit tricky credential-wise. If the real service runs under a normal user account, you can run the helper service under that account as well, and all should be OK as long as the account has appropriate access to the network share. If the real service will only work when run as LOCALSYSTEM or somesuch, things get more interesting, as it either won't be able to 'see' the network drive at all, or require some credential juggling to get things to work.

我发现了一个解决方案,它与使用psexec的解决方案类似,但不需要额外的工具就可以工作,并且可以在重新启动后继续工作。

只需添加一个计划任务,在“作为运行”字段中插入“system”,并使用简单的命令将任务指向一个批处理文件

net use z: \servername\sharedfolder /persistent:yes

然后选择“在系统启动时运行”(或类似的,我没有英文版本),你就完成了。

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

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

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

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

您既不想从“System”更改服务运行的用户,也不想找到一种狡猾的方式将映射运行为System。

有趣的是,这可以通过使用“at”命令,简单地将您的驱动器映射安排到未来一分钟,它将在系统帐户下运行,使驱动器对您的服务可见。