跟踪/调试 PowerShell 运算符



在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为例,但这也可能是&运算符以及它如何解析命令或其他东西。

不确定它是否有帮助,但-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.

最新更新