windows 如何在没有“不支持 UNC 路径”消息的情况下从网络共享运行批处理文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9013941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run batch file from network share without "UNC path are not supported" message?
提问by Stew
I am trying to run a batch file from a network share, but I keep getting the following message: "UNC path are not supported. Defaulting to Windows directory." The batch file is located on \\Server\Soft\WPX5\install.bat
. While logged in as administrator, from my Windows 7 Desktop, I navigate to \\Server\Soft\WP15\
and double click on install.bat, that's when I get the "UNC path are not supported." message. I found some suggestions online stating that mapping drive will not work, but using a symbolic link will solve this issue, but the symbolic link didn't work for me. Below is my batch file content, I would appreciate any assistance that can help me accomplish what I am trying to do. Basically, I want to be able to run the batch file from \\Server\Soft\WP15\install.bat
.
我正在尝试从网络共享运行批处理文件,但我不断收到以下消息:“不支持 UNC 路径。默认为 Windows 目录。” 批处理文件位于\\Server\Soft\WPX5\install.bat
. 以管理员身份登录时,从我的 Windows 7 桌面导航到\\Server\Soft\WP15\
并双击 install.bat,这时我得到“不支持 UNC 路径”。信息。我在网上找到了一些建议,指出映射驱动器不起作用,但使用符号链接可以解决这个问题,但符号链接对我不起作用。以下是我的批处理文件内容,我将不胜感激任何可以帮助我完成我正在尝试做的事情的帮助。基本上,我希望能够从\\Server\Soft\WP15\install.bat
.
Batch file content
批处理文件内容
mklink /d %userprofile%\Desktop\WP15 \server\soft\WP15
\server\soft\WP15\setup.exe
robocopy.exe "\server\soft\WP15\Custom" /copyall "C:\Program Files (x86)\WP\Custom Templates"
Regedit.exe /s \server\soft\WPX5\Custom\Migrate.reg
Also, how do I remove the symbolic link after the install is completed?
另外,安装完成后如何删除符号链接?
回答by dbenham
PUSHD and POPD should help in your case.
PUSHD 和 POPD 应该对您的情况有所帮助。
@echo off
:: Create a temporary drive letter mapped to your UNC root location
:: and effectively CD to that location
pushd \server\soft
:: Do your work
WP15\setup.exe
robocopy.exe "WP15\Custom" /copyall "C:\Program Files (x86)\WP\Custom Templates"
Regedit.exe /s WPX5\Custom\Migrate.reg
:: Remove the temporary drive letter and return to your original location
popd
Type PUSHD /?
from the command line for more information.
PUSHD /?
从命令行键入以获取更多信息。
回答by Vinzz
There's a registry settingto avoid this security check (use it at your own risks, though):
有一个注册表设置可以避免这种安全检查(尽管使用它需要您自担风险):
Under the registry path
HKEY_CURRENT_USER
\Software
\Microsoft
\Command Processoradd the value DisableUNCCheck REG_DWORD and set the value to 0 x 1 (Hex).
注册表路径下
HKEY_CURRENT_USER
\Software
\Microsoft
\Command Processor添加值 DisableUNCCheck REG_DWORD 并将值设置为 0 x 1(十六进制)。
Note:On Windows 10 version 1803, the setting seems to be located under HKLM: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor
注意:在 Windows 10 版本 1803 上,该设置似乎位于 HKLM 下:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor
回答by Grallen
I feel cls
is the best answer. It hides the UNC message before anyone can see it. I combined it with a @pushd %~dp0
right after so that it would seem like opening the script and map the location in one step, thus preventing further UNC issues.
我觉得cls
是最好的答案。它会在任何人看到之前隐藏 UNC 消息。我将它与一个@pushd %~dp0
right after结合起来,这样看起来就像打开脚本并一步映射位置,从而防止进一步的 UNC 问题。
cls
@pushd %~dp0
:::::::::::::::::::
:: your script code here
:::::::::::::::::::
@popd
Notes:
笔记:
pushd
will change your working directory to the scripts location in the new mapped drive.
pushd
将您的工作目录更改为新映射驱动器中的脚本位置。
popd
at the end, to clean up the mapped drive.
popd
最后,清理映射的驱动器。
回答by aphoria
Basically, you can't run it from a UNC path without seeing that message.
基本上,您不能在没有看到该消息的情况下从 UNC 路径运行它。
What I usually do is just put a CLS
at the top of the script so I don't have to see that message. Then, specify the full path to files in the network share that you need to use.
我通常做的只是CLS
在脚本的顶部放置一个,这样我就不必看到该消息。然后,指定您需要使用的网络共享中文件的完整路径。
回答by JayRO-GreyBeard
I needed to be able to just Windows Explorer browse through the server share, then double-click launch the batch file. @dbenham led me to an easier solution for my scenario (without the popd
worries):
我需要能够仅通过 Windows 资源管理器浏览服务器共享,然后双击启动批处理文件。@dbenham 让我找到了一个更简单的解决方案(无需popd
担心):
:: Capture UNC or mapped-drive path script was launched from
set NetPath=%~dp0
:: Assumes that setup.exe is in the same UNC path
%NetPath%setup.exe
:: Note that NetPath has a trailing backslash ("\")
robocopy.exe "%NetPath%Custom" /copyall "C:\Program Files (x86)\WP\Custom Templates"
Regedit.exe /s %NetPath%..\WPX5\Custom\Migrate.reg
:: I am not sure if WPX5 was typo, so use ".." for parent directory
set NetPath=
pause
回答by jameslr
Instead of launching the batch directly from explorer - create a shortcut to the batch and set the starting directory in the properties of the shortcut to a local path like %TEMP% or something.
不是直接从资源管理器启动批处理 - 创建批处理的快捷方式,并将快捷方式属性中的起始目录设置为本地路径,如 %TEMP% 或其他内容。
To delete the symbolic link, use the rmdir command.
要删除符号链接,请使用 rmdir 命令。
回答by Kai
I ran into the same issue recently working with a batch file on a network share drive in Windows 7.
我最近在处理 Windows 7 网络共享驱动器上的批处理文件时遇到了同样的问题。
Another way that worked for me was to map the server to a drive through Windows Explorer: Tools -> Map network drive. Give it a drive letter and folder path to \yourserver. Since I work with the network share often mapping to it makes it more convenient, and it resolved the “UNC path are not supported” error.
另一种对我有用的方法是通过 Windows 资源管理器将服务器映射到驱动器:工具 -> 映射网络驱动器。给它一个驱动器号和文件夹路径到 \yourserver。由于我经常使用网络共享映射到它,因此它更方便,并且它解决了“不支持 UNC 路径”错误。
回答by James
My situation is just a little different. I'm running a batch file on startup to distribute the latest version of internal business applications.
我的情况只是有点不同。我在启动时运行一个批处理文件来分发最新版本的内部业务应用程序。
In this situation I'm using the Windows Registry Run Key with the following string
在这种情况下,我使用带有以下字符串的 Windows 注册表运行键
cmd /c copy \serverName\SharedFolder\startup7.bat %USERPROFILE% & %USERPROFILE%\startup7.bat
This runs two commands on startup in the correct sequence. First copying the batch file locally to a directory the user has permission to. Then executing the same batch file. I can create a local directory c:\InternalApps and copy all of the files from the network.
这会在启动时以正确的顺序运行两个命令。首先将批处理文件本地复制到用户有权访问的目录。然后执行相同的批处理文件。我可以创建一个本地目录 c:\InternalApps 并从网络复制所有文件。
This is probably too late to solve the original poster's question but it may help someone else.
这可能为时已晚,无法解决原始海报的问题,但可能对其他人有所帮助。
回答by FreeSoftwareServers
This is the RegKey I used:
这是我使用的 RegKey:
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor]
"DisableUNCCheck"=dword:00000001
回答by Zhui Wang
My env windows10 2019 lts version and I add this two binray data ,fix this error
我的 env windows10 2019 lts 版本,我添加了这两个 binray 数据,修复了这个错误
Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor
DisableUNCCheck value 1
Computer\HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Command Processor
DisableUNCCheck value 1
Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor
DisableUNCCheck 值 1
Computer\HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Command Processor
DisableUNCCheck 值 1