跟踪/调试PowerShell运算符

时间:2016-03-19 07:37:32

标签: powershell debugging

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

1 个答案:

答案 0 :(得分: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