Author Posts

May 15, 2018 at 6:07 pm

I teach a PowerShell class and in a section on error handling, I discovered some strange behavior I can't explain. Please help if you can explain what is happening and why. Consider the following 2 functions (contrived to illustrate the issue).

function 1error { [CmdletBinding()] Param(); throw "This produces 1" }
function 2error { [CmdletBinding()] Param(); if($true) {throw "This produces 2"} }

If you call the function 1error with an error variable, the error variable will act as expected.

1error -ErrorVariable +1var
$1var.count #results in 1 

If you call function 2error with an error variable, the error variable will have 2 identical errors. Why?

2error -ErrorVariable +2var
$2var.count # results in 2 

PSVersion is 5.1.14393.2189

May 19, 2018 at 1:58 pm

So, this would be in a fresh console, and $2var ends up containing what exactly?

May 20, 2018 at 8:11 pm

That is interesting, I thought it might be because of the addition sign in front of the error variable or on of the 2's being construed as math, but even the changed code below shows the error variable containing 2 errors:

function Get-Error { [CmdletBinding()] Param(); if($true) {throw 'This produces 2'} }

Remove-Variable errVar -Force
$error.Clear()

Get-Error -ErrorVariable errvar
$errvar.count # results in 2 

Another interesting item is that the $Error variable only contains a single error:

PS C:\Users\Rob> $Error.Count
1

PS C:\Users\Rob> $errVar.Count
2

Although I don't understand the relevance of "if (true)", it should only execute a single time producing a single error, but I can reproduce what you are seeing in 5.1.17134.48.

May 21, 2018 at 2:55 am

$2var ends up containing two instances of the thrown error, as opposed to one as you would normally expect.

I'm starting to think this one is a bug.

May 21, 2018 at 12:51 pm

Thanks all for the replies. After more investigation, I do believe this is a bug. I found in Don Jones Big Book of PowerShell Error Handling , "As soon as terminating errors enter the picture, however, ErrorVariable has some very annoying behavior: it sometimes contains Exception objects instead of ErrorRecords, and in many cases, has one or more duplicate objects all relating to the terminating error." I believe this is the case in the code as it is producing duplicate objects relating to the terminating error. I posted a bug report in the Windows site. If I can verify it produces the same result in PS V6, I could post on GitHub as well. If you would like to vote this issue up on the Windows site, you can here https://windowsserver.uservoice.com/forums/301869-powershell/suggestions/34266901-terminating-error-in-if-statement-script-block-gen

May 21, 2018 at 1:00 pm

It is a fresh console in the code provided, but I can add

rv 1var, 2var

before calling the functions to continue testing without closing the shell and get the same result. It produces 2 identical ErrorRecord objects in the $2var variable but only one in $Error. I believe this may be a bug. I wanted to be sure there wasn't something I was missing before chalking it up to that, because I couldn't find a bug report already ID'd at Microsoft or GitHub for this specific issue.

May 21, 2018 at 4:32 pm

I'll double check when I get a chance, but I'm pretty sure when I tried it out it was on PS Core, one of the latest prereleases.

May 21, 2018 at 9:53 pm

Yeah still does this in PowerShell 6