假设一些Windows服务使用的代码需要映射网络驱动器,而不需要UNC路径。当服务启动时,如何使驱动器映射可用于服务的会话?作为服务用户登录并创建持久映射将不会在实际服务的上下文中建立映射。
当前回答
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.
希望这些解决方案对你有帮助。如果这对你有用,请告诉我。
其他回答
ForcePush,
注意:新创建的映射驱动器现在会出现在这个系统的所有用户面前,但他们会看到它显示为“断开连接的网络驱动器(Z:)”。别被名字骗了。它可能声称是断开连接的,但它对每个人都适用。这就是为什么你可以告诉这个黑客是不支持M$…
It all depends on the share permissions. If you have Everyone in the share permissions, this mapped drive will be accessible by other users. But if you have only some particular user whose credentials you used in your batch script and this batch script was added to the Startup scripts, only System account will have access to that share not even Administrator. So if you use, for example, a scheduled ntbackuo job, System account must be used in 'Run as'. If your service's 'Log on as: Local System account' it should work.
What I did, I didn't map any drive letter in my startup script, just used net use \\\server\share ... and used UNC path in my scheduled jobs. Added a logon script (or just add a batch file to the startup folder) with the mapping to the same share with some drive letter: net use Z: \\\... with the same credentials. Now the logged user can see and access that mapped drive. There are 2 connections to the same share. In this case the user doesn't see that annoying "Disconnected network drive ...". But if you really need access to that share by the drive letter not just UNC, map that share with the different drive letters, e.g. Y for System and Z for users.
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.
希望这些解决方案对你有帮助。如果这对你有用,请告诉我。
我发现了一个解决方案,它与使用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的建议,但稍微改变一下:创建自己的服务(让我们称之为映射服务)来映射驱动器,并将这个映射服务添加到第一个(实际工作的)服务的依赖项中。这样,在映射服务启动(并映射驱动器)之前,工作服务将不会启动。
更好的方法是使用mklink.exe使用符号链接。你可以在文件系统中创建一个任何应用程序都可以使用的链接。见http://en.wikipedia.org/wiki/NTFS_symbolic_link。
推荐文章
- 如何使用Windows命令行更改目录
- .msi和setup.exe文件之间的具体区别是什么?
- 如何在NERDTree中显示隐藏文件(以句点开始)?
- Objective-C for Windows
- “注册”一个.exe,这样你就可以从Windows中的任何命令行运行它
- Windows批处理文件的隐藏特性
- Windows递归grep命令行
- 如何创建自己的URL协议?(例如:/ /……)
- 在Windows批处理脚本中格式化日期和时间
- 映射一个网络驱动器供服务使用
- 如何在windows中使用命令提示符(cmd)列出文件。我试过在Linux中使用“ls”,但它显示一个错误?
- 如何从java应用程序创建一个windows服务
- Windows上最好的免费c++分析器是什么?
- 如何在Windows上运行多个Python版本
- 运行计划任务的最佳方式