windows 带双引号参数的批处理文件的管理员快捷方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2498087/
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
Administrator's shortcut to batch file with double quoted parameters
提问by XXB
Take an excruciatingly simple batch file:
以一个极其简单的批处理文件为例:
echo hi
pause
Save that as test.bat. Now, make a shortcut to test.bat. The shortcut runs the batch file, which prints "hi" and then waits for a keypress as expected. Now, add some argument to the target of the shortcut. Now you have a shortcut to:
将其另存为 test.bat。现在,创建 test.bat 的快捷方式。快捷方式运行批处理文件,打印“hi”,然后按预期等待按键。现在,向快捷方式的目标添加一些参数。现在你有一个快捷方式:
%path%\test.bat some args
The shortcut runs the batch file as before.
快捷方式像以前一样运行批处理文件。
Now, run the shortcut as administrator. (This is on Windows 7 by the way.) You can use either right-click -> Run as Administrator, or go to the shortcut's properties and check the box in the advanced section. Tell UAC that it's okay and once again the shortcut runs the batch file as expected.
现在,以管理员身份运行快捷方式。(顺便说一下,这是在 Windows 7 上。)您可以使用右键单击 -> 以管理员身份运行,或转到快捷方式的属性并选中高级部分中的框。告诉 UAC 没问题,快捷方式再次按预期运行批处理文件。
Now, change the arguments in the target of the shortcut to add double quotes:
现在,更改快捷方式目标中的参数以添加双引号:
%path%\test.bat "some args"
Now try the shortcut as administrator. It doesn't work this time! A command window pops up and and disappears too fast to see any error. I tried adding > test.log 2>&1 to the shortcut, but no log is created in this case.
现在以管理员身份尝试快捷方式。这次不行了!一个命令窗口弹出并消失得太快,看不到任何错误。我尝试将 > test.log 2>&1 添加到快捷方式,但在这种情况下没有创建日志。
Try running the same shortcut (with the double quotes) but not as Administrator. It runs the batch file fine. So, it seems the behavior is not because of the double quoted parameters, and it's not because it's run as Administrator. It's some weird combination of the two.
尝试运行相同的快捷方式(带双引号),但不要以管理员身份运行。它可以很好地运行批处理文件。因此,该行为似乎不是因为双引号参数,也不是因为它以管理员身份运行。这是两者的奇怪组合。
I also tried running the same command from an administrator's command window. This ran the batch file as expected without error. Running the shortcut from the command window spawned a new command window which flashed and went away. So apparently the issue is caused by a combination of administrator, the shortcut, and the double quotes.
我还尝试从管理员的命令窗口运行相同的命令。这按预期运行了批处理文件,没有错误。从命令窗口运行快捷方式会产生一个新的命令窗口,该窗口闪烁并消失。显然,该问题是由管理员、快捷方式和双引号的组合引起的。
I'm totally stumped, does anyone have any idea what's going on? I'd also like a workaround.
我完全被难住了,有人知道发生了什么吗?我也想要一个解决方法。
回答by Luke
I just ran Process Monitor on this and here is what I saw:
我刚刚运行了 Process Monitor,这是我看到的:
Run as User:
以用户身份运行:
cmd /c ""C:\Users\Sunbelt\Desktop\test.bat" "some args""
Run as Admin:
以管理员身份运行:
"C:\Windows\System32\cmd.exe" /C "C:\Users\Sunbelt\Desktop\test.bat" "some args"
For some reason, the Run as Admin case is not quoting the entire command. It seems it is trying to run the command:
出于某种原因,以管理员身份运行案例没有引用整个命令。似乎它正在尝试运行命令:
C:\Users\Sunbelt\Desktop\test.bat" "some args
I would guess that since the first space is quoted it actually trying to run the following command:
我猜因为第一个空格被引用,它实际上试图运行以下命令:
"C:\Users\Sunbelt\Desktop\test.bat some" args
And in Process Monitor logs there is a file system entry of "NO SUCH FILE" for "C:\Users\Sunbelt\Desktop\test.bat some". I don't know why it is different when run as Admin, but that's what appears to be happening.
在进程监视器日志中,“C:\Users\Sunbelt\Desktop\test.bat some”有一个“NO SUCH FILE”的文件系统条目。我不知道为什么以管理员身份运行时会有所不同,但这似乎正在发生。
回答by Kjell Rilbe
To work around this, create another bat file on a path without spaces, and with a filename without spaces, that simply does this:
要解决此问题,请在不带空格的路径上创建另一个 bat 文件,并使用不带空格的文件名,只需执行以下操作:
call "Long path to original bat file\Original bat file.bat"
This secondary bat file can be run as admin.
此辅助 bat 文件可以以管理员身份运行。
You can now create a shortcut to this secondary bat file and check run as admin in the shortcut's advanced options. The shortcut can be placed on a path with spaces and it can have a filename containing spaces.
您现在可以创建此辅助 bat 文件的快捷方式,并在快捷方式的高级选项中检查以管理员身份运行。快捷方式可以放置在带有空格的路径上,并且它的文件名可以包含空格。
回答by angelgarcan
In my case I just want to pass one filename as parameter, but the path has spaces.
在我的情况下,我只想传递一个文件名作为参数,但路径有空格。
I found a solution that worked for this case (if that's okay to truncate the file name).
我找到了一个适用于这种情况的解决方案(如果可以截断文件名)。
Create another bat file (input_gate.bat) to remove the spaces in the path using the syntax of CALL.exe. Assuming that the shortcut is named test.lnk and is on the same route as the input_gate.bat:
创建另一个 bat 文件 (input_gate.bat) 以使用 CALL.exe 的语法删除路径中的空格。假设快捷方式名为 test.lnk 并且与 input_gate.bat 在同一路径上:
call %~sdp0test.lnk %~sf1
This pass as a parameter to test.bat the full file name in short format, with administrator privileges.
这作为参数传递给 test.bat 短格式的完整文件名,具有管理员权限。
- %~sdp0 -> Is the current path (for the input_gate.bat) in short format.
- %~sf1 -> Is the first parameter passed to input_gate.bat (in my case the full filename with spaces)
- %~sdp0 -> 是短格式的当前路径(对于 input_gate.bat)。
- %~sf1 -> 是传递给 input_gate.bat 的第一个参数(在我的例子中是带空格的完整文件名)
回答by FrinkTheBrave
This worked for me in Windows 7:
这在 Windows 7 中对我有用:
ShortcutTarget: C:\Windows\System32\cmd.exe /C myscript.bat Param1 "Param Two with spaces"
StartIn: "C:\Path containing\my script"
Not tried it yet as Admin. I don't think it would work if myscript.bat contained spaces
尚未以管理员身份尝试过。如果 myscript.bat 包含空格,我认为它不会起作用
回答by JohnK
I finally figured it out, in a way that allows the use of long filenames (short filenames weren't adequate for my use case). My solution works on Win 7, 8, and 10. I think it was inspired by Luke's mention of the missing double-quote.
我终于想通了,以一种允许使用长文件名的方式(短文件名不适用于我的用例)。我的解决方案适用于 Win 7、8 和 10。我认为它的灵感来自 Luke 提到丢失的双引号。
Here it is:
这里是:
1.) For the shortcut you create to the batch file, which you set to run as admin, use the following command line:
1.) 对于您创建的批处理文件的快捷方式,您设置为以管理员身份运行,请使用以下命令行:
cmd /s /c ""path_to_batch_file"
Note that there are 2 double-quote characters at the beginning of the command, and only 1 at the end, even though there shouldnormally be 2 at the end also. But that is the trick in getting it to work!
请注意,命令开头有 2 个双引号字符,末尾只有 1 个,尽管通常末尾也应该有 2 个。但这就是让它工作的诀窍!
2.) In your batch file, add back the missing double-quote character:
2.) 在您的批处理文件中,重新添加缺少的双引号字符:
set user_file=%1"
That's it! Here's an example to let you see it in action:
就是这样!这是一个示例,可让您看到它的实际效果:
1.) On your desktop, create "test.bat" with the following content:
1.) 在您的桌面上,使用以下内容创建“test.bat”:
@echo off
set user_file=%1"
echo The file is: %user_file%
pause
3.) Create a shortcut to the batch file. Set it to run as admin, and give it the following command line:
3.) 创建批处理文件的快捷方式。将其设置为以管理员身份运行,并为其提供以下命令行:
cmd /s /c ""%userprofile%\desktop\test.bat"
4.) Drag a file onto the shortcut. Success! (I hope...)
4.) 将文件拖到快捷方式上。成功!(我希望...)
回答by David
Answer here worked for me: https://superuser.com/questions/931003/passing-variables-with-space-to-the-command-line-as-admin
这里的答案对我有用:https: //superuser.com/questions/931003/passing-variables-with-space-to-the-command-line-as-admin
Which is...
这是...
Adding cmd /C
before the target.
I also had to make sure my script's name and path didn't have spaces, and not
quote the script's path in target.
cmd /C
在目标之前添加。我还必须确保我的脚本的名称和路径没有空格,并not
在target 中引用脚本的路径。