映射服务使用的网络驱动器

发布于 2024-07-07 03:17:07 字数 109 浏览 19 评论 0原文

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

Suppose some Windows service uses code that wants mapped network drives and no UNC paths. How can I make the drive mapping available to the service's session when the service is started? Logging in as the service user and creating a persistent mapping will not establish the mapping in the context of the actual service.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

铃予 2024-07-14 03:17:07

使用此功能需要您自担风险。 (我已经在 XP 和 Server 2008 x64 R2 上进行了测试)

对于此 hack,您将需要 SysinternalsSuite,作者:Mark Russinovich

第一步:
打开提升的 cmd.exe 提示符(以管理员身份运行)

第二步:
使用 PSExec.exe 再次提升至 root:
导航到包含 SysinternalsSuite 的文件夹并执行以下命令
psexec -i -s cmd.exe
您现在处于 nt Authority\system 提示符中,您可以通过输入 whoami 来证明这一点。 需要 -i 是因为驱动器映射需要与用户交互

第三步:
使用以下命令将持久映射驱动器创建为 SYSTEM 帐户
net use z: \\servername\sharedfolder /persistent:yes

就这么简单!

警告:您只能按照创建映射的方式从 SYSTEM 帐户中删除此映射。 如果需要删除它,请按照步骤 1 和 2 操作,但将步骤 3 中的命令更改为 net use z: /delete

注意:新创建的映射驱动器现在将显示给该系统的所有用户,但他们会看到它显示为“断开的网络驱动器 (Z:)”。 不要让这个名字欺骗你。 它可能声称已断开连接,但它适用于所有人。 这就是您如何判断 M$ 不支持此 hack 的方法。

Use this at your own risk. (I have tested it on XP and Server 2008 x64 R2)

For this hack you will need SysinternalsSuite by Mark Russinovich:

Step one:
Open an elevated cmd.exe prompt (Run as administrator)

Step two:
Elevate again to root using PSExec.exe:
Navigate to the folder containing SysinternalsSuite and execute the following command
psexec -i -s cmd.exe
you are now inside of a prompt that is nt authority\system and you can prove this by typing whoami. The -i is needed because drive mappings need to interact with the user

Step Three:
Create the persistent mapped drive as the SYSTEM account with the following command
net use z: \\servername\sharedfolder /persistent:yes

It's that easy!

WARNING: You can only remove this mapping the same way you created it, from the SYSTEM account. If you need to remove it, follow steps 1 and 2 but change the command on step 3 to net use z: /delete.

NOTE: The newly created mapped drive will now appear for ALL users of this system but they will see it displayed as "Disconnected Network Drive (Z:)". Do not let the name fool you. It may claim to be disconnected but it will work for everyone. That's how you can tell this hack is not supported by M$.

老子叫无熙 2024-07-14 03:17:07

我找到了一种与 psexec 类似的解决方案,但无需额外工具即可工作,并且可以在重新启动后继续运行

只需添加一个计划任务,在“运行方式”字段中插入“系统”,然后使用简单的命令将任务指向批处理文件

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

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

I found a solution that is similar to the one with psexec but works without additional tools and survives a reboot.

Just add a sheduled task, insert "system" in the "run as" field and point the task to a batch file with the simple command

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

Then select "run at system startup" (or similar, I do not have an English version) and you are done.

仅冇旳回忆 2024-07-14 03:17:07

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

辅助进程方法可以非常简单:只需创建一个映射驱动器的新服务并启动“真正的”服务。 唯一不完全无关紧要的事情是:

  • 帮助程序服务需要将所有适当的 SCM 命令(启动/停止等)传递给实际服务。 如果真正的服务接受自定义 SCM 命令,请记住也传递这些命令(不过,我不希望认为 UNC 路径奇异的服务使用此类命令...)

  • 事情可能会变得有点棘手的凭据 -明智的。 如果真实服务在普通用户帐户下运行,您也可以在该帐户下运行帮助程序服务,只要该帐户具有对网络共享的适当访问权限,一切都应该没问题。 如果真正的服务只有在作为 LOCALSYSTEM 或类似的东西运行时才能工作,事情就会变得更有趣,因为它要么根本无法“看到”网络驱动器,要么需要一些凭据杂耍才能让事情正常工作。 p>

You'll either need to modify the service, or wrap it inside a helper process: apart from session/drive access issues, persistent drive mappings are only restored on an interactive logon, which services typically don't perform.

The helper process approach can be pretty simple: just create a new service that maps the drive and starts the 'real' service. The only things that are not entirely trivial about this are:

  • 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.

寄离 2024-07-14 03:17:07

更好的方法是使用 mklink.exe 来使用符号链接。 您只需在文件系统中创建任何应用程序都可以使用的链接即可。 请参阅http://en.wikipedia.org/wiki/NTFS_symbolic_link

A better way would be to use a symbolic link using mklink.exe. You can just create a link in the file system that any app can use. See http://en.wikipedia.org/wiki/NTFS_symbolic_link.

留蓝 2024-07-14 03:17:07

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

即您可以使用符号链接,例如

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

There is a good answer here:
https://superuser.com/a/651015/299678

I.e. You can use a symbolic link, e.g.

mklink /D C:\myLink \\127.0.0.1\c$
第七度阳光i 2024-07-14 03:17:07

您可以使用“net use”命令:

var p = System.Diagnostics.Process.Start("net.exe", "use K: \\\\Server\\path");
var isCompleted = p.WaitForExit(5000);

如果这在服务中不起作用,请尝试 Winapi 和 PInvoke WNetAddConnection2

编辑: 显然我误解了你 - 你不能更改服务的源代码,对吧? 在这种情况下,我会遵循 mdb 的建议,但有一点改动:创建您自己的服务(我们称之为映射服务)映射驱动器并将此映射服务添加到第一个(实际工作)服务的依赖项中。 这样,在映射服务启动(并映射驱动器)之前,工作服务不会启动。

You could us the 'net use' command:

var p = System.Diagnostics.Process.Start("net.exe", "use K: \\\\Server\\path");
var isCompleted = p.WaitForExit(5000);

If that does not work in a service, try the Winapi and PInvoke WNetAddConnection2

Edit: Obviously I misunderstood you - you can not change the sourcecode of the service, right? In that case I would follow the suggestion by mdb, but with a little twist: Create your own service (lets call it mapping service) that maps the drive and add this mapping service to the dependencies for the first (the actual working) service. That way the working service will not start before the mapping service has started (and mapped the drive).

心头的小情儿 2024-07-14 03:17:07

我找到了一个非常简单的方法:使用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:

I find a very simple method: using command "New-SmbGlobalMapping" of powershell, which will mount drive globally:

$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:
旧时光的容颜 2024-07-14 03:17:07

ForcePush,

注意:新创建的映射驱动器现在将向该系统的所有用户显示,但他们会看到它显示为“断开连接的网络驱动器 (Z:)”。 不要让这个名字欺骗你。 它可能声称已断开连接,但它适用于所有人。 这就是您如何判断 M$ 不支持此黑客的方法...

这完全取决于共享权限。如果您拥有“每个人”的共享权限,则其他用户将可以访问此映射驱动器。 但是,如果您只有某个特定用户,您在批处理脚本中使用了其凭据,并且该批处理脚本已添加到启动脚本中,则只有系统帐户才能访问该共享,甚至管理员也无法访问该共享。
因此,如果您使用计划的 ntbackuo 作业,则必须在“运行方式”中使用系统帐户。
如果您的服务的“登录身份:本地系统帐户”它应该可以工作。

我做了什么,我没有在启动脚本中映射任何驱动器号,只是使用了 net use \\\server\share ... 并在我的启动脚本中使用了 UNC 路径预定的工作。 添加了登录脚本(或者只是将批处理文件添加到启动文件夹),并将其映射到具有某些驱动器号的相同共享:net use Z: \\\... 具有相同的凭据。 现在登录的用户可以查看并访问该映射驱动器。 有 2 个连接到同一共享。 在这种情况下,用户不会看到烦人的“断开网络驱动器...”。 但是,如果您确实需要通过驱动器号而不仅仅是 UNC 来访问该共享,请使用不同的驱动器号来映射该共享,例如 Y 代表系统,Z 代表用户。

ForcePush,

NOTE: The newly created mapped drive will now appear for ALL users of this system but they will see it displayed as "Disconnected Network Drive (Z:)". Do not let the name fool you. It may claim to be disconnected but it will work for everyone. That's how you can tell this hack is not supported by 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.

够运 2024-07-14 03:17:07

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

以带有 NFS 磁盘的 Windows Server 2012 为例:

第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。

打开“任务计划程序”,创建一个新任务:

  1. 在“系统启动”时以“SYSTEM”身份运行。
  2. 创建操作:运行“C:\mount_nfs.bat”。

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

Found a way to grant Windows Service access to Network Drive.

Take Windows Server 2012 with NFS Disk for example:

Step 1: Write a Batch File to Mount.

Write a batch file, ex: 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

Step 2: Mount Disk as NT AUTHORITY/SYSTEM.

Open "Task Scheduler", create a new task:

  1. Run as "SYSTEM", at "System Startup".
  2. Create action: Run "C:\mount_nfs.bat".

After these two simple steps, my Windows ActiveMQ Service run under "Local System" priviledge, perform perfectly without login.

惟欲睡 2024-07-14 03:17:07

当您通常从命令提示符运行可执行文件时,您能够访问驱动器的原因是,当您将其作为普通 exe 执行时,您正在使用您登录的用户帐户运行该应用程序。 并且该用户具有访问网络的权限。 但是,当您将可执行文件安装为服务时,默认情况下,如果您在任务管理中看到它在“SYSTEM”帐户下运行。 您可能知道“系统”无权访问网络资源。

这个问题可以有两种解决方案。

  1. 将驱动器映射为上面已经指出的持久驱动器。

  2. 还有一种可以遵循的方法。 如果您通过键入“services.msc”打开服务管理器,您可以转到您的服务,并且在服务的属性中,有一个登录选项卡,您可以在其中将帐户指定为“系统”之外的任何其他帐户,您可以从您自己登录的用户帐户或通过“网络服务”启动服务。 当您执行此操作时..该服务可以访问任何网络组件和驱动器,即使它们也不是持久的。
    要以编程方式实现此目的,您可以查看“CreateService”函数:
    http://msdn.microsoft.com/en- us/library/ms682450(v=vs.85).aspx 并可以将参数 'lpServiceStartName ' 设置为 'NT AUTHORITY\NetworkService'。 这将在“网络服务”帐户下启动您的服务,然后您就完成了。

  3. 还可以尝试通过在 CreateService() 函数的 servicetype 参数标志中指定 SERVICE_INTERACTIVE_PROCESS 来使服务成为交互式服务,但这仅限于 XP,因为 Vista 和 7 不支持此功能。

希望这些解决方案对您有所帮助。让我知道这是否对您有用。

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.

There can be two solutions to this problem.

  1. To map the drive as persistent as already pointed above.

  2. 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.

  3. 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.

Hope the solutions help you.. Let me know if this worked for you .

泪冰清 2024-07-14 03:17:07

您既不想更改服务在“系统”下运行的用户,也不想找到一种偷偷摸摸的方式来以系统身份运行映射。

有趣的是,这可以通过使用 "at" 命令来实现,只需安排您的驱动器映射到未来一分钟,它将在系统帐户下运行,使驱动器对您的服务可见。

You wan't to either change the user that the Service runs under from "System" or find a sneaky way to run your mapping as System.

The funny thing is that this is possible by using the "at" command, simply schedule your drive mapping one minute into the future and it will be run under the System account making the drive visible to your service.

止于盛夏 2024-07-14 03:17:07

我还不能发表评论(致力于声誉),但创建了一个帐户只是为了回答@Tech Jerk @spankmaster79(好名字哈哈)和@NMC 他们报告的问题,以回复“我找到了一个与以下解决方案类似的解决方案” psexec 但不需要额外的工具即可工作,并且可以在重新启动后继续存在。” @Larry 发布的帖子。

解决方案是从登录帐户中浏览到该文件夹​​,即:

    \\servername\share  

并让它提示登录,然后输入您在 psexec 中用于 UNC 的相同凭据。 之后它开始工作。 就我而言,我认为这是因为提供服务的服务器不是与我映射到的服务器同一域的成员。 我在想,如果 UNC 和计划任务都引用 IP 而不是主机名,

    \\123.456.789.012\share 

则可以完全避免该问题。

如果我在这里获得足够的代表点,我会将其添加为回复。

I can't comment yet (working on reputation) but created an account just to answer @Tech Jerk @spankmaster79 (nice name lol) and @NMC issues they reported in reply to the "I found a solution that is similar to the one with psexec but works without additional tools and survives a reboot." post @Larry had made.

The solution to this is to just browse to that folder from within the logged in account, ie:

    \\servername\share  

and let it prompt to login, and enter the same credentials you used for the UNC in psexec. After that it starts working. In my case, I think this is because the server with the service isn't a member of the same domain as the server I'm mapping to. I'm thinking if the UNC and the scheduled task both refer to the IP instead of hostname

    \\123.456.789.012\share 

it may avoid the problem altogether.

If I ever get enough rep points on here i'll add this as a reply instead.

雪花飘飘的天空 2024-07-14 03:17:07

我想在最受接受的答案中添加一些内容。 就我而言,我正在安装天蓝色文件共享,我需要提供连接凭据。
下面是我在 bat 文件中使用的脚本

@echo off
net use z: \\sampleblobstorage.file.core.windows.net\logs /u:sampleblobstorage <storage-account-key> /persistent:yes

使用运行帐户 SYSTEM 从任务管理器运行此 .bat 文件

I would like to add something to the most accepted answer. In my case I was mounting azure file share, Where i need to provide the credentials to connect.
Below is the script I used in a bat file

@echo off
net use z: \\sampleblobstorage.file.core.windows.net\logs /u:sampleblobstorage <storage-account-key> /persistent:yes

Run this .bat file from Task Manager using the run as account SYSTEM

静待花开 2024-07-14 03:17:07

您可以将脚本设置为在每次使用驱动器时映射/取消映射驱动器,而不是依赖持久驱动器:

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

这对我有用。

Instead of relying on a persistent drive, you could set the script to map/unmap the drive each time you use it:

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

This works for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文