Open
Description
PowerShell classes requires variables to be initialized in a method before reference. This causes issues with global variables without scoping as the parser will throw a Variable is not assigned in the method
error. To get around this the variable must be scoped but this causes PSScriptAnalyzer to throw a violation. The rule should only trigger when assigning a variable to the global scope not reading.
Steps to reproduce
class Test {
[void] Abc() {
if ($global:PSEdition -eq 'core') { write-host core }
}
}
Expected behavior
No warnings
Actual behavior
RuleName Severity ScriptName Line Message
-------- -------- ---------- ---- -------
PSAvoidGlobalVars Warning 3 Found global variable 'global:PSEdition'.
If an unexpected error was thrown then please report the full error details using e.g. $error[0] | Select-Object *
Environment data
7.6-preview4
1.24.0
Metadata
Metadata
Assignees
Labels
No labels