【问题标题】:Trace/debug PowerShell operators跟踪/调试 PowerShell 运算符
【发布时间】: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


【解决方案1】:

不确定是否有帮助,但 -lt-gt 是不区分大小写的运算符,它们的行为类似于 -ilt 和 -igt。如果你想要区分大小写的运算符,你应该使用-clt-cgt

这是我在 PowerShell 5.0 中获得的结果,我不确定它是否有帮助

Trace-Command -PSHost -Name TypeMatch, TypeConversion -Expression { "Less" -lt "less" }
DÉBOGUER : TypeConversion Information: 0 : Converting "System.Object[]" to "System.Object".
DÉBOGUER : TypeConversion Information: 0 :     Result type is assignable from value to convert's type
DÉBOGUER : TypeConversion Information: 0 : Converting "" to "System.String".
DÉBOGUER : TypeConversion Information: 0 :     Result type is assignable from value to convert's type
DÉBOGUER : TypeConversion Information: 0 : Converting "" to "System.String".
DÉBOGUER : TypeConversion Information: 0 :     Result type is assignable from value to convert's type
DÉBOGUER : TypeConversion Information: 0 : Converting "System.Management.Automation.InvocationInfo" to "System.Management.Automation.InvocationInfo".
DÉBOGUER : TypeConversion Information: 0 :     Result type is assignable from value to convert's type
DÉBOGUER : TypeConversion Information: 0 : Converting "System.Object[]" to "System.Object[]".
DÉBOGUER : TypeConversion Information: 0 :     Result type is assignable from value to convert's type
False

如果我使用-cgt,我会获得相同的跟踪,但结果是True

【讨论】:

  • 嗯.. 我已经在运行 PS 5.0 的三台不同的计算机上尝试过,但它没有返回任何内容。猜猜是时候进行全新安装了。这有点回答了这个问题,但我希望能更深入一点。前任。能够为操作员获取调用堆栈或其他东西,这样我就可以看到调用了哪个类、方法等。也许这是不可能的,但我已经习惯于对像你这样的人在 SO 上的知识感到惊讶,所以我会再等一会儿,看看是否有人有一个神奇的解决方案。 :-) 谢谢
  • 从 Windows PowerShell v5.1 / PowerShell Core v6.0-alpha.17 开始,上述命令根本不产生跟踪输出。此外,此答案中显示的跟踪输出(例如,可在 Windows PowerShell v5.0.10586.117 中获得)不包含有关脚本块中特定表达式的信息 - 如果您传递 {},则会得到完全相同的跟踪输出,即, empty 脚本块。
猜你喜欢
  • 2016-10-15
  • 1970-01-01
  • 1970-01-01
  • 2020-08-22
  • 2023-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多