powershell - 为什么尾随反斜杠会破坏 Powershell 参数解析?

标签 powershell parameters quoting

<分区>

我一直在绞尽脑汁试图找出 Powershell 忽略命令行参数的原因,我想我终于发现字符串参数上的尾部反斜杠以某种方式混淆了参数解析。

我有

param (
    [Parameter(Mandatory=$true)][String]$p1,
    [Parameter(Mandatory=$true)][String]$p2
)

Write-Output "p1:$p1, p2:$p2"

最终,我需要从 Windows 任务调度程序调用类似这样的东西,这意味着使用 powershell.exe 作为程序并将此脚本和参数添加为 args。

在生产中,p1 是一个文件路径。所以我尝试:

PS C:\source> C:\Windows\System32\windowspowershell\v1.0\powershell.exe -ExecutionPolicy Bypass -File  .\myScript.ps1 -p1 'C:\Program Files\' -p2 asdf

cmdlet myScript.ps1 at command pipeline position 1
Supply values for the following parameters:
p2:

为什么提示?参数就在那里。我最终无意中删除了 p1 的结尾反斜杠:

PS C:\source> C:\Windows\System32\windowspowershell\v1.0\powershell.exe -ExecutionPolicy Bypass -File  .\myScript.ps1 -p1 'C:\Program Files' -p2 asdf
p1:C:\Program Files, p2:asdf

这是一个错误吗?

最佳答案

这在 cmd 中对我有用(双反斜杠和双引号)。我猜反斜杠是 powershell 命令行选项的转义字符。

C:\Windows\System32\windowspowershell\v1.0\powershell.exe -ExecutionPolicy Bypass -file .\myScript.ps1 -p1 "C:\Program Files\\" -p2 asdf

p1:C:\Program Files\, p2:asdf

关于powershell - 为什么尾随反斜杠会破坏 Powershell 参数解析?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60384660/

相关文章:

powershell - ScriptBlock 中的变量 - Powershell

C# 多个参数合二为一以消除参数传递

c++ - std::vector::push_back 参数引用

mysql - 将文本插入到恰好包含 MySQL 中的 sql 的 varchar 中?

powershell - 在PowerShell中运行命令行

powershell - “kubectl修补程序”在Linux Bash上有效,但在Windows Powershell ISE中不可用

exception - 如何在 PowerShell 中配置调用深度?

powershell - 使用 PowerShell 将文件从工作站复制到服务器,保留目录结构

.htaccess -> 域参数 -> https ://example. com/page 正在工作,而 https ://example. com/没有

azure - Microsoft Teams 访问策略(即 New-CsApplicationAccessPolicy 命令)存在问题