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

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

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(其内置别名为 startsaps),请将其用 \&quot;...&quot;\\ 转义是必要的,以便传递给 powershell.exe,即 Windows PowerShell CLI 的(隐含的)-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.html
匿名

发表评论

匿名网友

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

确定