1

I ran into a problem I simply cannot understand. I wrote a script that uses custom Classes to handle different objects. It runs perfectly on my work machine (PowerShell 5.1) on my personal laptop (PowerShell 5.1), on every other colleague's PC, but on one of my colleague's PC (PowerShell 5.0) it does something strange. When she runs the script, it throws variable not assigned exception. The strange part? It throws it for every single $Error[0] in my script. Of course, I didn't reference to $Error[0] anywhere outside the catch part of a try-catch block, so it shouldn't exist unless there is an exception. What makes the whole thing even more strange is when the script is initiated, neither of the classes that contain a try-catch block with the $Error variable is called. The script works on every other machine I tried, this one is the only exception. I guess it has something to do with the the PowerShell version, as every other machine has PowerShell 5.1 while hers have 5.0. Did the PowerShell prior 5.1 have this behavior? Checking the contents of an uncalled try-catch block within an uncalled Class? The same thing happens even when I write a script that's as simple as this one:

Class Foo { Foo() { try { Write-Host "Bar" } catch { Write-Host $error[0] } } } Write-Host "Start program" 

I don't call the class, and even if I called it, it shouldn't throw an exception, yet on that single machine, running this script throws "variable not assigned in Method" exception. On her machine, even PowerShell ISE underlines the $error variable with red, and writes the same error message. But only on her machine, it happens on neither else.

6
  • 1
    Can you please show us the code that results in this behavior? Commented Nov 23, 2020 at 14:27
  • 1
    Could be caused by different settings in profile: check $ErrorActionPreference and Set-StrictMode. Commented Nov 23, 2020 at 14:33
  • I added some code to the post. Commented Nov 23, 2020 at 14:50
  • Does this answer your question: Why can't I use predefined variables in class methods? Commented Nov 23, 2020 at 15:23
  • @iRon , I also suspected scoping, but I'm unsure how well it fits the OP's description. I'd assume the variable isn't referenced until the catch is triggered. Scoping would mean there is a local version of the variable at that point. Then why would there be an undefined variable error? Withstanding something anomalous about $error, and given we know the behavior doesn't occur in 5.1, I think the fair conclusion is a difference in behavior, possibly a 5.0 bug fixed in 5.1+... Commented Nov 23, 2020 at 16:12

1 Answer 1

1

@vonPryz's comment/explanation seems plausible. However, I tested both settings $ErrorActionPreference = "Stop" & Set-StrictMode -Version 5.0 (also 5.1). Neither produced the specific error you describe. It's worth pointing out that $Error[0] returns an "Index was out of range..." error when Strict Mode is on and the collection is empty. It doesn't return "variable undefined", as would be the most recognizable symptom of strict mode.

I can only conclude this is specific to the version, and perhaps a bug was fixed 5.0 - 5.1. Note: 5.0 is no longer supported. inconvenient as it may be you're probably going to have to upgrade at least for the audience of this script. If that's not possible, you may have to refactor around the shortcoming.

Sign up to request clarification or add additional context in comments.

4 Comments

Ever since I figured out it happens whenever there is an unassigned variable in a class method. Not just $Error but any kind of it. The part I don't understand is why does PowerShell bother checking the validity of a variable within a class that isn't even called? Or at least was it a regular behavior in 5.0, or is it some machine specific thing? It's more of a curiosity to me, I don't really plan to spend time to get around the problem. We'll update PowerShell on her machine, and I'm pretty sure it'll solve the whole thing, I'm just curious.
Well a class is a definition, so PowerShell must have a look at the code before using it. However, I share your confusion as the same would be true for functions and scripts blocks, but with no such restriction.
This is not a 5.0 bug (at least not the v 5.0.10586.0 from Windows 10 1511, I cannot try the first Windows 10 version - only those two ones have v 5.0). The sample script is running well on this version, and when you call this class, it displays 'Bar' correctly, I tested with a Throw in the Try block and `$error[0]' is displayed... Pretty strange behavior you have got
@CFou appreciate the refinement. I don't have any 5.0.xx to test with. I'd imagine if it were a bug with a new feature as were classes at the time it would be fixed relatively quick, perhaps explaining the point version deferential in behavior...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.