【发布时间】:2016-07-06 01:56:00
【问题描述】:
在 PowerShell 中,我可以使用 Trace-Command 来解决参数绑定、类型转换等问题。例如:
Trace-Command -PSHost -Name ParameterBinding -Expression { $null = "c:\" | dir}
...
DEBUG: ParameterBinding Information: 0 : Parameter [Path] PIPELINE INPUT ValueFromPipeline NO COERCION
DEBUG: ParameterBinding Information: 0 : BIND arg [c:\] to parameter [Path]
DEBUG: ParameterBinding Information: 0 : Binding collection parameter Path: argument type [String], parameter type [System.String[]], collection type Array, eleme
nt type [System.String], no coerceElementType
...
在 PS 中调试一些奇怪的行为时,我想跟踪 -lt 比较的工作原理(可能每个字符都转换为 [int][char]"x" 等)。我尝试使用Trace-Command,但它没有返回任何内容。
Trace-Command -PSHost -Name TypeMatch, TypeConversion -Expression { "Less" -lt "less" }
#No trace-output, only returned value
False
#Get any type of trace-informatino
Trace-Command -PSHost -Name * -Expression { "Less" -lt "less" }
#No trace-output, only returned value
False
有什么办法可以查出这些内部操作员是如何在幕后工作的?追踪信息?详细输出?我以-lt 和-gt 为例,但这也可能是&-operator 以及它如何解析命令或其他内容。
【问题讨论】:
-
我也有类似的问题。我已经实现了我自己的类,它继承了 DynamicObject、IEnumerable 并覆盖了 TryBinaryOperation。但是,如果我的类型的最左边的操作数看起来 PS 比较运算符根本不会调用 TryBinaryOperator 方法,而是如果右边的操作数是字符串,则调用 GetEnumerator。我需要弄清楚比较运算符的 AST PS 构建是什么,以便为我的自定义类型正确实现它们。
标签: powershell debugging