英文:
Pass argument(file-path) that contains spaces to the Powershell command inside Batch file that itself resides at path with spaces
问题
我在我的批处理脚本中有以下几行代码,它们调用Powershell命令来将批处理文件自动提升为管理员权限。
@echo off & setlocal
if not "%1"=="admin" (powershell start -verb runas '%0' admin & pause & exit /b)
....
....
但是,批处理文件路径本身以及'%0'中的%USERNAME%(Vivek Shah)包含空格,因此在调用此脚本时会抛出错误:
The term 'C:\Users\Vivek' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.
At line:1 char:1
我该如何处理'%0'中的空格,使得脚本能够正常工作?
我已经尝试过'%~0'和'"%0"',但似乎都不起作用。
英文:
I have following lines in my Batch script which in turn calls Powershell command to self-elevate the batch file as Admin.
@echo off && setlocal
if not "%1"=="admin" (powershell start -verb runas '%0' admin & pause & exit /b)
....
....
But the Batch file path itself and as result the '%0' contains spaces in the %USERNAME%(Vivek Shah) especially, and that's why when invoking this script, it throws error:
The term 'C:\Users\Vivek' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.
At line:1 char:1
How do I handle spaces in '%0' and make this script working perfectly ??
Already tried '%~0' & '"%0"' but both apparently don't work.
答案1
得分: 1
<!-- language-all: sh -->
-
使用
%~f0来引用正在运行的批处理文件的完整路径。- 运行
cmd /c call /?以了解语法的解释。
- 运行
-
为了确保此路径原样传递给 PowerShell 的
Start-Process(其内置别名为start和saps),请将其用\"..."\(\转义是必要的,以便传递给powershell.exe,即 Windows PowerShell CLI 的(隐含的)-Command参数将"字符作为要执行的命令的一部分传递)。<sup>[1]</sup>
@echo off && setlocal
if not "%1"=="admin" (powershell start -verb runas \"%~f0\" admin & pause & exit /b)
<sup>[1] 注意:如果不将隐含的 -Command 参数用 "..." 包围起来,整体上意味着 ~%f0 的值将受到 空白规范化 的影响,这意味着多个空白字符的连续运行将被折叠为每个空格一个。然而,在实践中很少会出现这个问题。只是添加一个整体的 "..." 包围(powershell "start ..." & pause & exit /b)通常是不够的,不幸的是,因为它需要在 \"...\" 内部逐个进行 ^ 转义以转义任何 cmd.exe 元字符。在极少数情况下,如果空白规范化是一个问题,你可以在整体的 "..." 内部使用 "^""..."^""(错误)来引用,但只能在 for /f 循环之外 使用 - 参见 此答案。幸运的是,在 PowerShell (Core) 7+ 中,其 CLI 是 pwsh.exe,这个健壮的解决方案更简单:在 for /f 之外和之内都使用整体的 "..." 包围,并嵌入 ""..."" 引用。</sup>
英文:
<!-- language-all: sh -->
-
Use
%~f0to refer to the running batch file's full path.- Run
cmd /c call /?for an explanation of the syntax.
- Run
-
To ensure that this path is passed as-is to PowerShell's
Start-Process(whose built-in aliases arestartandsaps), enclose it in\"..."\(the\-escaping is necessary in order for the (implied)-Commandargument passed topowershell.exe, the Windows PowerShell CLI, to pass the"characters through as part of the command to execute).<sup>[1]</sup>
@echo off && setlocal
if not "%1"=="admin" (powershell start -verb runas \"%~f0\" admin & pause & exit /b)
<sup>[1] Note: Without enclosing the implied -Command argument in "..." overall means that the value of ~%f0 is subject to whitespace normalization, meaning that runs of multiple whitespace characters are folded into a single space each. However, that is rarely a problem in practice. Just adding an overall "..." enclosure (powershell "start ..." & pause & exit /b) is not necessarily enough, unfortunately, because it requires individually ^-escaping any cmd.exe metacharacters inside \"...\". In the rare event that whitespace normalization is a problem, with powershell.exe you can use "^""..."^"" (sic) inside overall "...", but only outside for /f loops - see this answer. Fortunately, in PowerShell (Core) 7+, whose CLI is pwsh.exe, the robust solution is simpler: both outside and inside for /f, use overall "..." enclosure with embedded ""..."" quoting.</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论