I'm having a weird problem where I get different behavior when setting "Set-PSDebug -Trace 2".
I traced it down to a switch statement not executing properly and was able to reproduce it on Powershell V3 but not on Powershell V2 or Powershell V1 (works as expected)
Take the following simple function:
function DoTest {
$result = "Switch Case Not Executed"
$VendorName = "Microsoft"
switch ($VendorName)
{
"Microsoft" { $result = "Switch Case Executed" }
}
Write-host "Switch: $($VendorName) -> $result"
}
Now run the following:
#Works as expected
Set-PSDebug -Off; DoTest;
#Doesn't work as expected
Set-PSDebug -Trace 2; DoTest;
Results on PosH V3 with PSDebug Trace
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+ >>>> }
In PoSH version 3, even the debug tracing indicates that the value is set, but it seems to skip the switch statement entirely. I even tried the Set-StrictMode
and everything runs fine. It's only when I enable PSDebug tracing. Is this behavior intended?