英文:
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
-
使用
%~f0
来引用正在运行的批处理文件的完整路径。- 运行
cmd /c call /?
以了解语法的解释。
- 运行
-
为了确保此路径原样传递给 PowerShell 的
Start-Process
(其内置别名为start
和saps
),请将其括在\"..."\
中(\
转义是必需的,以便传递给powershell.exe
,Windows PowerShell 命令行界面的(隐式的)-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
%~f0
to 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 arestart
andsaps
), enclose it in\"..."\
(the\
-escaping is necessary in order for the (implied)-Command
argument 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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论