Skip to content

Rule PSUseDeclaredVarsMoreThanAssignments generates false positives (warnings) where it should not #201

Closed
@KirkMunro

Description

@KirkMunro

Problem #1:
It is common in psm1 files to define variables that are used by functions in the rest of the module. These module-scope variables are often initialized and not used in the psm1 file itself. Yet running Invoke-ScriptAnalyzer against a psm1 file that defines these variables and properly initialized them causes warnings to be generated using this rule.

Solution #1:
Once Invoke-ScriptAnalyzer supports invocation against modules (as proposed in #199), this rule should check more broadly to see if variables that are declared and initialized in a script module (psm1) file are used in other files in the module. These variables that are in "script" scope can be found by looking for $script:variableName or by looking for references to Get-/Set-/Remove-Variable -Scope Script -Name variableName.

Problem #2:
This rule generates a warning against a function that sets a built-in variable using the script scope. I have the following line in one of my functions:
$Script:PSDefaultParameterValues['Get-DoPxWebRequestHeader:AccessToken'] = $AccessToken

Solution #2:
That line is perfectly valid, and it is not initializing a variable that is never used because it is a built-in variable. This rule needs to ignore built-in variables, whether they are scoped (script/global, or using numeric scopes with Get-/Set-Variable in the -Scope parameter) or not.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions