Pass argument(file-path) that contains spaces to the Powershell command inside Batch file that itself resides at path with spaces

huangapple go评论82阅读模式
英文:

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(其内置别名为 startsaps),请将其括在 \&quot;...&quot;\ 中(\ 转义是必需的,以便传递给 powershell.exe,Windows PowerShell 命令行界面的(隐式的)-Command 参数作为要执行的命令的一部分将 &quot; 字符传递)。<sup>[1]</sup>

@echo off &amp;&amp; setlocal

if not &quot;%1&quot;==&quot;admin&quot; (powershell start -verb runas \&quot;%~f0\&quot; admin &amp; pause &amp; exit /b)

<sup>[1] 注意:如果不将隐式的 -Command 参数括在 &quot;...&quot; 中,那么 ~%f0 的值将受到空白规范化的影响,这意味着多个空白字符会被折叠为一个空格。然而,在实践中很少会出现这个问题。不幸的是,仅仅添加整体的 &quot;...&quot; 封装 (powershell &quot;start ...&quot; &amp; pause &amp; exit /b) 不一定足够,因为它需要单独对 \&quot;...\&quot; 中的任何 cmd.exe 元字符进行 ^ 转义。在极少数情况下,如果空白规范化是一个问题,你可以在整体的 &quot;...&quot; 中使用 &quot;^&quot;&quot;...&quot;^&quot;&quot;(即使如此)封装,但只能在**for /f 循环之外**使用 - 参见此答案。幸运的是,在PowerShell (Core) 7+,其CLI是pwsh.exe,稳健的解决方案更简单:在for /f 循环之外和之内都使用整体的 &quot;...&quot; 封装,同时嵌套使用 &quot;&quot;...&quot;&quot; 引用。</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.
  • To ensure that this path is passed as-is to PowerShell's Start-Process (whose built-in aliases are start and saps), enclose it in \&quot;...&quot;\ (the \-escaping is necessary in order for the (implied) -Command argument passed to powershell.exe, the Windows PowerShell CLI, to pass the &quot; characters through as part of the command to execute).<sup>[1]</sup>

@echo off &amp;&amp; setlocal

if not &quot;%1&quot;==&quot;admin&quot; (powershell start -verb runas \&quot;%~f0\&quot; admin &amp; pause &amp; exit /b)

<sup>[1] Note: Without enclosing the implied -Command argument in &quot;...&quot; 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 &quot;...&quot; enclosure (powershell &quot;start ...&quot; &amp; pause &amp; exit /b) is not necessarily enough, unfortunately, because it requires individually ^-escaping any cmd.exe metacharacters inside \&quot;...\&quot;. In the rare event that whitespace normalization is a problem, with powershell.exe you can use &quot;^&quot;&quot;...&quot;^&quot;&quot; (sic) inside overall &quot;...&quot;, 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 &quot;...&quot; enclosure with embedded &quot;&quot;...&quot;&quot; quoting.</sup>

huangapple
  • 本文由 发表于 2023年8月9日 08:32:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76863874-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定