似乎有人巧妙地改变了在 powershell 上解析参数开关的方式。在某些机器上,“split-path c:\x\y --parent”有效。在某些情况下它失败了。谁能告诉我 a) 是什么导致了差异 b) 我该如何阻止它?
Switch 参数在 V1 和 V2 中应该以相同的方式工作(这意味着-parent是正确的语法)。
在您的情况下,--parent应该将参数作为字符串绑定。它不应被解释为开关。您可以通过测试绑定Trace-Command
Trace-Command parameterbinding -Expression { split-path c:\x\y --parent} -PSHost
Run Code Online (Sandbox Code Playgroud)
更多信息:
考虑--:后面的每个字符串都--被解释为参数,无论它是否看起来像一个开关。
[14]: function test {
param([switch]$sw, [string]$str)
write-host switch is $sw
write-host str is $str
}
[15]: test 1
switch is False
str is 1
[16]: test -sw
switch is True
str is
[17]: test -- -sw
switch is False
str is -sw
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1910 次 |
| 最近记录: |