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


当前回答

这里有一个很好的答案: https://superuser.com/a/651015/299678

例如,你可以使用符号链接。

mklink /D C:\myLink \\127.0.0.1\c$

其他回答

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

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

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

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

更好的方法是使用mklink.exe使用符号链接。你可以在文件系统中创建一个任何应用程序都可以使用的链接。见http://en.wikipedia.org/wiki/NTFS_symbolic_link。

这里有一个很好的答案: https://superuser.com/a/651015/299678

例如,你可以使用符号链接。

mklink /D C:\myLink \\127.0.0.1\c$

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

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.

找到一种方法授予Windows服务访问网络驱动器。

以Windows Server 2012自带NFS磁盘为例:

步骤1:写一个批处理文件挂载。

写一个批处理文件,例如:C:\mount_nfs.bat

echo %time% >> c:\mount_nfs_log.txt
net use Z: \\{your ip}\{netdisk folder}\ >> C:\mount_nfs_log.txt 2>&1

步骤2:将磁盘挂载为NT AUTHORITY/SYSTEM。

打开“Task Scheduler”,创建一个新任务:

运行“系统”,在“系统启动”。 创建操作:运行"C:\mount_nfs.bat"。

经过这两个简单的步骤,我的Windows ActiveMQ服务在“本地系统”特权下运行,无需登录即可完美执行。