具有双引号参数的批处理文件的管理员快捷方式

发布于 2024-08-25 18:47:46 字数 808 浏览 3 评论 0原文

采取一个极其简单的批处理文件:

echo hi
pause

将其另存为 test.bat。现在,创建 test.bat 的快捷方式。该快捷方式运行批处理文件,该文件打印“hi”,然后按预期等待按键。现在,向快捷方式的目标添加一些参数。现在您有一个快捷方式:

%path%\test.bat some args

该快捷方式像以前一样运行批处理文件。

现在,以管理员身份运行快捷方式。 (顺便说一句,这是在 Windows 7 上。)您可以使用右键单击 ->以管理员身份运行,或转到快捷方式的属性并选中高级部分中的框。告诉 UAC 没问题,快捷方式将再次按预期运行批处理文件。

现在,更改快捷方式目标中的参数以添加双引号:

%path%\test.bat "some args"

现在以管理员身份尝试该快捷方式。这次不行了!命令窗口弹出和消失的速度太快,看不到任何错误。我尝试添加 > test.log 2>&1 到快捷方式,但在这种情况下不会创建日志。

尝试运行相同的快捷方式(带双引号),但不要以管理员身份运行。它可以很好地运行批处理文件。因此,该行为似乎不是因为双引号参数,也不是因为它以管理员身份运行。这是两者的一些奇怪的组合。

我还尝试从管理员命令窗口运行相同的命令。这按预期运行了批处理文件,没有错误。从命令窗口运行快捷方式会生成一个新的命令窗口,该窗口闪烁并消失。显然,这个问题是由管理员、快捷方式和双引号的组合引起的。

我完全困惑了,有人知道发生了什么事吗?我也想要一个解决方法。

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:

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

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.

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.

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

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

发布评论

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

评论(6

好倦 2024-09-01 18:47:46

我刚刚对此运行了进程监视器,这是我所看到的:

以用户身份运行:

cmd /c ""C:\Users\Sunbelt\Desktop\test.bat" "some args""

以管理员身份运行:

"C:\Windows\System32\cmd.exe" /C "C:\Users\Sunbelt\Desktop\test.bat" "some args"

出于某种原因,以管理员身份运行案例没有引用整个命令。看起来它正在尝试运行该命令:

C:\Users\Sunbelt\Desktop\test.bat" "some args

我猜想,由于引用了第一个空格,它实际上试图运行以下命令:

"C:\Users\Sunbelt\Desktop\test.bat some" args

并且在进程监视器日志中,有一个文件系统条目“C:没有这样的文件” \Users\Sunbelt\Desktop\test.bat 一些”。我不知道为什么以管理员身份运行时会有所不同,但这似乎就是发生的情况。

I just ran Process Monitor on this and here is what I saw:

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.

忆悲凉 2024-09-01 18:47:46

要解决此问题,请在不带空格的路径上创建另一个 bat 文件,并且文件名不带空格,只需执行以下操作:

call "Long path to original bat file\Original bat file.bat"

此辅助 bat 文件可以以管理员身份运行。

您现在可以创建此辅助 bat 文件的快捷方式,并在快捷方式的高级选项中选中以管理员身份运行。快捷方式可以放置在带有空格的路径上,并且它的文件名可以包含空格。

To work around this, create another bat file on a path without spaces, and with a filename without spaces, that simply does this:

call "Long path to original bat file\Original bat file.bat"

This secondary bat file can be run as admin.

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.

忆梦 2024-09-01 18:47:46

就我而言,我只想传递一个文件名作为参数,但路径有空格。

我找到了适用于这种情况的解决方案(如果可以截断文件名)。

创建另一个 bat 文件 (input_gate.bat),以使用 CALL.exe 的语法删除路径中的空格。
假设快捷方式名为 test.lnk 并且与 input_gate.bat 位于同一路径上:

call %~sdp0test.lnk %~sf1

这会将短格式的完整文件名作为参数传递给 test.bat,并具有管理员权限。

  • %~sdp0->是短格式的当前路径(对于 input_gate.bat)。
  • %~sf1​​->是传递给 input_gate.bat 的第一个参数(在我的例子中是带空格的完整文件名)

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:

call %~sdp0test.lnk %~sf1

This pass as a parameter to test.bat the full file name in short format, with administrator privileges.

  • %~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)
落叶缤纷 2024-09-01 18:47:46

这在 Windows 7 中对我有用:

ShortcutTarget: C:\Windows\System32\cmd.exe /C myscript.bat Param1 "Param Two with spaces"
StartIn:        "C:\Path containing\my script"

尚未以管理员身份尝试过。我认为如果 myscript.bat 包含空格,它不会起作用

This worked for me in 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

在梵高的星空下 2024-09-01 18:47:46

我终于想通了,以一种允许使用长文件名的方式(短文件名不足以满足我的用例)。我的解决方案适用于 Win 7、8 和 10。我认为它的灵感来自 Luke 提到的缺少双引号。

如下:

1.) 对于您创建的批处理文件的快捷方式(设置为以管理员身份运行),请使用以下命令行:

cmd /s /c ""path_to_batch_file"

请注意,命令开头有 2 个双引号字符,并且末尾只有 1,尽管通常末尾也应该有 2。但这就是让它发挥作用的秘诀!

2.) 在批处理文件中,添加回缺少的双引号字符:

set user_file=%1"

就是这样!下面是一个示例,可让您看到它的实际效果:

1.) 在桌面上,创建包含以下内容的“test.bat”:

@echo off
set user_file=%1"
echo The file is: %user_file%
pause

3.) 创建批处理文件的快捷方式。将其设置为以管理员身份运行,并为其提供以下命令行:

cmd /s /c ""%userprofile%\desktop\test.bat"

4.) 将文件拖到快捷方式上。成功! (我希望...)

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.

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:

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 should normally be 2 at the end also. But that is the trick in getting it to work!

2.) In your batch file, add back the missing double-quote character:

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:

@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:

cmd /s /c ""%userprofile%\desktop\test.bat"

4.) Drag a file onto the shortcut. Success! (I hope...)

丢了幸福的猪 2024-09-01 18:47:46

这里的答案对我有用:https://superuser。 com/questions/931003/passing-variables-with-space-to-the-command-line-as-admin

这是...

之前添加 cmd /C目标。
我还必须确保脚本的名称和路径没有空格,并且目标中引用脚本的路径。

Answer here worked for me: 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.

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