假设一些Windows服务使用的代码需要映射网络驱动器,而不需要UNC路径。当服务启动时,如何使驱动器映射可用于服务的会话?作为服务用户登录并创建持久映射将不会在实际服务的上下文中建立映射。
当前回答
我发现了一个非常简单的方法:使用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:
其他回答
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.
您既不想从“System”更改服务运行的用户,也不想找到一种狡猾的方式将映射运行为System。
有趣的是,这可以通过使用“at”命令,简单地将您的驱动器映射安排到未来一分钟,它将在系统帐户下运行,使驱动器对您的服务可见。
我还不能评论(在声誉方面),但创建了一个帐户,只是为了回答@Tech Jerk @spankmaster79(好名字lol)和@NMC问题,他们在回复“我发现了一个解决方案,与psexec类似,但不需要额外的工具,并在重启后幸存。”
解决方案是在登录帐户中浏览到该文件夹,即:
\\servername\share
并让它提示登录,并输入您在psexec中用于UNC的相同凭据。之后就开始工作了。在我的例子中,我认为这是因为提供服务的服务器与我要映射到的服务器不属于同一个域。我在想,如果UNC和计划任务都是指IP而不是主机名
\\123.456.789.012\share
它可能会完全避免这个问题。
如果我曾经得到足够的代表点在这里,我会把这作为一个回复。
而不是依赖于一个持久驱动器,你可以设置脚本映射/取消映射驱动器每次你使用它:
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.
推荐文章
- 在Sublime Text 2中使用Ctrl+D进行多个选择时,我如何跳过匹配?
- #定义WIN32_LEAN_AND_MEAN具体排除了什么?
- 最好的轻量级web服务器(只有静态内容)的Windows
- npm犯错!错误:EPERM:操作不允许,重命名
- 崇高的文本从命令行
- 如何将MongoDB作为Windows服务运行?
- 如何保持Python脚本输出窗口打开?
- 使用.bat文件检查文件夹是否存在
- 如何查看Windows库(*.lib)的内容
- 在Windows中删除大文件夹最快的方法是什么?
- NPM卡住给出相同的错误EISDIR:对目录的非法操作,读取错误(本机)
- Windows 10环境变量太大
- 如何使用Windows命令行更改目录
- .msi和setup.exe文件之间的具体区别是什么?
- 如何在NERDTree中显示隐藏文件(以句点开始)?