【问题标题】:How can I run a PowerShell script with white spaces in the path from the command line?如何在命令行的路径中运行带有空格的 PowerShell 脚本?
【发布时间】:2018-01-27 08:45:39
【问题描述】:

所以我尝试了很多不同的方法来从命令行运行 PowerShell 脚本,每一个都返回一个错误。

这是这条路:

C:\Users\test\Documents\test\line space\PS Script\test.ps1

我试过这些:

powershell -File '"C:\Users\test\Documents\test\line space\PS Script\test.ps1"'

powershell "& ""C:\Users\test\Documents\test\line space\PS Script\test.ps1"""

Powershell "& 'C:\Users\test\Documents\test\line space\PS Script\test.ps1'"

Powershell -File 'C:\Users\test\Documents\test\line space\PS Script\test.ps1'"

我得到了所有这些错误:

& : 术语“C:\Users\test\Documents\test\line space\PS Script”未被识别为 cmdlet、函数、脚本文件或可运行程序的名称。检查名称的拼写,如果包含路径,请验证路径是否正确,然后重试。

处理 -File ''C:\Users\test\Documents\test\line space\PS Script'' 失败:不支持给定路径的格式。为 -File 参数指定一个有效的路径。

我该如何解决这个问题?

【问题讨论】:

  • 你为什么从 cmd 运行它?

标签: powershell cmd scripting


【解决方案1】:

-File 参数

如果你想从命令行运行powershell.exe -File,你总是需要用双引号(") 来设置路径。单引号 (') 只能被 PowerShell 识别。但是由于命令行调用了 powershell.exe(并因此处理了文件参数),因此您必须使用 "

powershell.exe -File "C:\Users\test\Documents\Test Space\test.ps1" -ExecutionPolicy Bypass

-Command 参数

如果您使用-Command 参数,而不是-File,则-Command 内容由PowerShell 处理。因此,您可以 - 在这种情况下必须 - 在 " 中使用 '

powershell.exe -Command "& 'C:\Users\test\Documents\Test Space\test.ps1'" -ExecutionPolicy Bypass

双引号是命令行处理的,& 'C:\Users\test\Documents\Test Space\test.ps1'是PowerShell实际处理的命令。

方案 1 显然更简单。

请注意,-Command 也是使用的默认参数,如果您未指定任何参数。

powershell.exe "& 'C:\Users\test\Documents\Test Space\test.ps1'" -ExecutionPolicy Bypass

这也行。

-EncodedCommand 参数

您可以将您的命令编码为Base64。这解决了许多“引用”问题,有时(但不是你的情况)是唯一可能的方法。

首先你必须创建编码命令

$Command = "& 'C:\Users\test\Documents\Test Space\test.ps1'"
[Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($Command))

然后你可以像这样使用-EncodedCommand 参数

powershell.exe -EncodedCommand JgAgACcAQwA6AFwAVQBzAGUAcgBzAFwAdABlAHMAdABcAEQAbwBjAHUAbQBlAG4AdABzAFwAVABlAHMAdAAgAFMAcABhAGMAZQBcAHQAZQBzAHQALgBwAHMAMQAnAA== -ExecutionPolicy Bypass

【讨论】:

  • 干得好;顺便说一句:在 PowerShell Core 中,-File 现在是默认值(此更改是支持 Unix shebang 行所必需的)。
  • 我觉得这是一个比接受的答案更好的答案。仅使用 -file 参数将文件路径放在引号中对我来说根本不起作用。
【解决方案2】:

试试这个:

& "C:\Users\test\Documents\test\line space\PS Script\test"

【讨论】:

  • 他正在尝试使用cmd,所以我假设他是从.cmd.bat 文件中调用它。 PS Invoke (&) 运算符对他没有任何好处。
  • PS C:\> & "C:\Temp\Folder with Whitespaces\SomeApp.exe" /switchA valA /switchB valB 将在 PowerShell 中使用&符号作为前缀。没有与号,它将出错。对于 DOS 控制台,与号不是必需的。我认为这就是 C.Hennessey 的发展方向。
【解决方案3】:

在您的示例中,您无缘无故地混合了引号和双引号。

IF EXIST "C:\Users\test\Documents\test\line space\PS Script\test.ps1" (
  powershell -ExecutionPolicy Unrestricted -File "C:\Users\test\Documents\test\line space\PS Script\test.ps1"
)

【讨论】:

  • 这对我不起作用,脚本没有触发。我尝试只运行这部分:powershell -ExecutionPolicy Unrestricted -File 'C:\Users\test\Documents\test\line space\PS Script\test.ps1' 并得到这个错误:不支持给定路径的格式。跨度>
  • @Awsmike 尝试删除单引号,看看会发生什么
  • 处理 - 文件 C:\Users\test\Documents\test\line space\PS Script\test.ps1 失败,因为该文件没有“.ps1”扩展名。请指定一个有效的 Windows PowerShell 脚本文件名,然后重试。
  • 好的,所以它将空格解释为文件的额外参数。路径首先有效吗? @Awsmike
  • 你原来用双引号代替单引号的答案确实有效!
【解决方案4】:

如果您使用参数,您可以执行以下操作。

powershell.exe -command "& {&'C:\A B C\foo.ps1' param1 param2}"

此时感谢a blog post by Hesham A. Amin :-)

【讨论】:

    【解决方案5】:

    我需要传递带有空格的参数

    我正在将一个文件拖放到一个批处理文件中,但该文件在服务器上已关闭,路径和/或文件名中有空格。在测试了上述答案之后,我得到了这个工作。请注意,在启动 PowerShell 可执行文件之前,我将更改为工作目录。

    批处理文件:

    pushd O:\Data\QuickList
    start powershell -noexit -Command ".\QuickList.ps1 -datafile '%1'"
    popd
    

    【讨论】:

      猜你喜欢
      • 2021-11-22
      • 2021-08-25
      • 1970-01-01
      • 1970-01-01
      • 2015-07-15
      • 1970-01-01
      • 2022-10-05
      • 2021-08-28
      • 1970-01-01
      相关资源
      最近更新 更多