假设一些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.
其他回答
你可以使用'net use'命令:
var p = System.Diagnostics.Process.Start("net.exe", "use K: \\\\Server\\path");
var isCompleted = p.WaitForExit(5000);
如果这在服务中不起作用,请尝试Winapi和PInvoke WNetAddConnection2
编辑:显然我误解你了——你不能改变服务的源代码,对吗?在这种情况下,我会遵循mdb的建议,但稍微改变一下:创建自己的服务(让我们称之为映射服务)来映射驱动器,并将这个映射服务添加到第一个(实际工作的)服务的依赖项中。这样,在映射服务启动(并映射驱动器)之前,工作服务将不会启动。
更好的方法是使用mklink.exe使用符号链接。你可以在文件系统中创建一个任何应用程序都可以使用的链接。见http://en.wikipedia.org/wiki/NTFS_symbolic_link。
而不是依赖于一个持久驱动器,你可以设置脚本映射/取消映射驱动器每次你使用它:
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
这对我很有用。
您要么需要修改服务,要么将其包装到辅助进程中:除了会话/驱动器访问问题外,持久驱动器映射仅在交互式登录时恢复,而服务通常不会执行。
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.
使用此信息的风险自负。(我已经在XP和Server 2008 x64 R2上进行了测试)
对于这个hack,你将需要SysinternalsSuite由Mark Russinovich:
第一步: 打开提升的cmd.exe提示符(以管理员身份运行)
第二步: 使用PSExec.exe再次提升到根目录: 导航到包含SysinternalsSuite的文件夹并执行以下命令 Psexec -i -s cmd.exe 您现在处于nt authority\系统的提示符中,您可以通过键入whoami来证明这一点。需要使用-i是因为驱动器映射需要与用户交互
第三步: 使用以下命令将持久映射驱动器创建为SYSTEM帐户 Net使用z: \\servername\sharedfolder /persistent:yes
就是这么简单!
警告:您只能从SYSTEM帐户中删除与创建映射相同的映射。如果您需要删除它,请按照步骤1和步骤2执行,但将步骤3中的命令更改为net use z: /delete。
注意:新创建的映射驱动器现在会出现在这个系统的所有用户面前,但他们会看到它显示为“断开连接的网络驱动器(Z:)”。别被名字骗了。它可能声称是断开连接的,但它对每个人都适用。这就是为什么你可以告诉这个黑客是不支持的M$。
推荐文章
- XAMPP -端口80被PID 4的“无法打开进程”使用!12
- 为什么git在Windows下记不住我的密码
- Git克隆/拉不断冻结在“存储密钥在缓存?”
- 有没有替换Windows (Visual C)的unistd.h ?
- 从Windows批处理文件设置系统环境变量?
- 在Sublime Text 2中使用Ctrl+D进行多个选择时,我如何跳过匹配?
- #定义WIN32_LEAN_AND_MEAN具体排除了什么?
- 最好的轻量级web服务器(只有静态内容)的Windows
- npm犯错!错误:EPERM:操作不允许,重命名
- 崇高的文本从命令行
- 如何将MongoDB作为Windows服务运行?
- 如何保持Python脚本输出窗口打开?
- 使用.bat文件检查文件夹是否存在
- 如何查看Windows库(*.lib)的内容
- 在Windows中删除大文件夹最快的方法是什么?