开发者

How to stop Powershell insisting on one dash in parameters?

开发者 https://www.devze.com 2023-01-03 09:48 出处:网络
It seems that somebody 开发者_如何学Pythonhas subtly changed the way that parameter switches are parsed on powershell.On some machines \"split-path c:\\x\\y --parent\" works.On some it fails.Can anyon

It seems that somebody 开发者_如何学Pythonhas subtly changed the way that parameter switches are parsed on powershell. On some machines "split-path c:\x\y --parent" works. On some it fails. Can anyone tell me a) what causes the difference and b) how can I stop it?


Switch parameters should work in the same way in both V1 and V2 (that means -parent is the right syntax).

In your case --parent should be bound to an parameter as a string. It should not be interpreted as a switch. You can test the binding via Trace-Command

Trace-Command parameterbinding -Expression { split-path c:\x\y --parent} -PSHost

Further info:

Considering --: every string behind -- is interpreted as argument, no matter if it looks like a switch.

[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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号