如何阻止 Powershell 在参数中坚持一个破折号? [英] How to stop Powershell insisting on one dash in parameters?

查看:39
本文介绍了如何阻止 Powershell 在参数中坚持一个破折号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎有人巧妙地改变了在powershell上解析参数开关的方式.在某些机器上,split-path c:\x\y --parent"有效.在某些情况下它失败了.谁能告诉我 a) 是什么导致了差异 b) 我该如何阻止它?

It seems that somebody has 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?

推荐答案

切换参数在 V1 和 V2 中应该以相同的方式工作(这意味着 -parent 是正确的语法).

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

在您的情况下 --parent 应该作为字符串绑定到参数.它不应被解释为开关.您可以通过 Trace-Command

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

更多信息:

考虑到 --:-- 后面的每个字符串都被解释为参数,无论它是否看起来像一个开关.

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

这篇关于如何阻止 Powershell 在参数中坚持一个破折号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆