<分区>
我一直在绞尽脑汁试图找出 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
这是一个错误吗?