【发布时间】:2013-09-14 07:42:12
【问题描述】:
我遇到了一个奇怪的问题,即在设置“Set-PSDebug -Trace 2”时出现不同的行为。
我将其追溯到一个未正确执行的 switch 语句,并且能够在 Powershell V3 上重现它,但不能在 Powershell V2 或 Powershell V1 上重现(按预期工作)
取如下简单函数:
function DoTest {
$result = "Switch Case Not Executed"
$VendorName = "Microsoft"
switch ($VendorName)
{
"Microsoft" { $result = "Switch Case Executed" }
}
Write-host "Switch: $($VendorName) -> $result"
}
现在运行以下命令:
#Works as expected
Set-PSDebug -Off; DoTest;
#Doesn't work as expected
Set-PSDebug -Trace 2; DoTest;
带有 PSDebug Trace 的 PosH V3 上的结果
DEBUG: 3+ Set-PSDebug -Trace 2; >>>> DoTest;
DEBUG: 1+ function DoTest >>>> {
DEBUG: ! CALL function 'DoTest'
DEBUG: 2+ >>>> $result = "Switch Case Not Executed"
DEBUG: ! SET $result = 'Switch Case Not Executed'.
DEBUG: 3+ >>>> $VendorName = "Microsoft"
DEBUG: ! SET $VendorName = 'Microsoft'.
DEBUG: ! SET $switch = 'Microsoft'.
DEBUG: 4+ switch ( >>>> $VendorName)
DEBUG: ! SET $switch = ''.
DEBUG: 9+ >>>> Write-host "Switch: $($VendorName) -> $result"
DEBUG: 9+ Write-host "Switch: $( >>>> $VendorName) -> $result"
Switch: Microsoft -> Switch Case Not Executed
DEBUG: 11+ >>>> }
在 PoSH 版本 3 中,即使是调试跟踪也表明该值已设置,但似乎完全跳过了 switch 语句。我什至尝试了Set-StrictMode,一切运行良好。只有当我启用 PSDebug 跟踪时。这种行为是有意的吗?
【问题讨论】:
标签: powershell switch-statement script-debugging