在文件路径中运行带有空格的 PowerShell 脚本

Posted

技术标签:

【中文标题】在文件路径中运行带有空格的 PowerShell 脚本【英文标题】:Run PowerShell script with spaces in the file path 【发布时间】:2021-11-22 22:19:45 【问题描述】:

如果变量 !batDir! 包含空格,我无法使以下批处理代码工作,例如W:\scripts windows\:

if /I "!i!" GTR "1" (
        Powershell "!batDir!ript.ps1 !test! '!testWithSpaces!' "
        goto :scriptEnd
)

W:\scripts : The term 'W:\scripts' is not recognized as the name of a cmdlet, function, script file, or operable
program.

【问题讨论】:

您是否尝试过'!batDir!ript.ps1',即文件路径周围的单引号,例如'!testWithSpaces!' 然后我得到一个错误Unexpected token '-filter' in expression or statement.变量!test!是-过滤器。 我认为这意味着路径已经合格。我建议先echo 表达式,然后再将其传递给 Powershell 进行调试。 Powershell "'W:\scrips windows\ript.ps1' -filter 'unity*' " 请参阅this 从 CMD 运行 PS。 【参考方案1】:

您需要在 & 中使用引号。

Powershell "& '!batDir!ript.ps1' !test! '!testWithSpaces!' "

https://newbedev.com/how-to-run-a-powershell-script-with-white-spaces-in-path-from-command-line

【讨论】:

以上是关于在文件路径中运行带有空格的 PowerShell 脚本的主要内容,如果未能解决你的问题,请参考以下文章