假设一些Windows服务使用的代码需要映射网络驱动器,而不需要UNC路径。当服务启动时,如何使驱动器映射可用于服务的会话?作为服务用户登录并创建持久映射将不会在实际服务的上下文中建立映射。
当前回答
找到一种方法授予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服务在“本地系统”特权下运行,无需登录即可完美执行。
其他回答
您既不想从“System”更改服务运行的用户,也不想找到一种狡猾的方式将映射运行为System。
有趣的是,这可以通过使用“at”命令,简单地将您的驱动器映射安排到未来一分钟,它将在系统帐户下运行,使驱动器对您的服务可见。
我发现了一个非常简单的方法:使用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:
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
然后选择“在系统启动时运行”(或类似的,我没有英文版本),你就完成了。
找到一种方法授予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服务在“本地系统”特权下运行,无需登录即可完美执行。
推荐文章
- 如何在Windows命令提示符下运行.sh ?
- 如何从命令行在windows中找到mysql数据目录
- 在没有事件源注册的情况下写入Windows应用程序事件日志
- 无法在Windows上从/usr/local/ssl/openssl.cnf加载配置信息
- GIT克隆在windows中跨本地文件系统回购
- 如何运行一个PowerShell脚本而不显示窗口?
- PowerShell:仅为单个命令设置环境变量
- 为什么这个Windows批处理文件只执行第一行,而在命令shell中执行所有三行?
- 环境变量存储在Windows注册表的哪里?
- 有一个好的Valgrind Windows的替代品吗?
- Windows和Linux上的c++编译:ifdef开关
- 如何验证批处理文件中是否存在一个文件?
- XAMPP -端口80被PID 4的“无法打开进程”使用!12
- 为什么git在Windows下记不住我的密码
- Git克隆/拉不断冻结在“存储密钥在缓存?”