Here is my testing script:
trap {"trapped"}
if ($true) {
nonsenseString1
exit 0
}
nonsenseString2
and the result is:
trapped
nonsenseString1 : The term 'nonsenseString1' is not recognized ...
trapped
nonsenseString2 : The term 'nonsenseString2' is not recognized ...
If this not what I wanted and what I expected. After it trapped nonsenseString1, it should exit 0, why it doesn't? This happens in both powershell 5 and 7.
I discovered this has to do with the if conditional block. If I take off the block, then it behaves as I expected.
PS: My real purpose was to record any errors that may happen on a Powershell script, and people tell me that I should use Start-Transcript:
How do I redirect stdout and stderr inside a powershell script?
Since I do not know when the error happens, I want to Stop-Transcript in a trap.