Skip to content

PSUseDeclaredVarsMoreThanAssignments false positive when used via Get-Variable #831

Closed
@thomasrayner

Description

@thomasrayner

The PSUseDeclaredVarsMoreThanAssignments rule will erroneously throw a warning if a variable is declared but only used via the Get-Variable cmdlet.

Steps to reproduce

Create a .ps1 file with the following contents.

$variable = 'a value'
Write-Output "The value is $((Get-Variable -Name 'variable').Value)"

Examine the file with PSScriptAnalyzer.

Invoke-ScriptAnalyzer -Path .\pssa-demo.ps1 -IncludeRule 'PSUseDeclaredVarsMoreThanAssignments'

A warning will be created.

RuleName                            Severity     ScriptName Line  Message
--------                            --------     ---------- ----  -------
PSUseDeclaredVarsMoreThanAssignments Warning      pssa-demo. 1     The variable 'variable' is assigned but never used.
                                                ps1

Screenshot

image

This is in addition to the issues this rules has when splatting or using in Invoke-Command, foreach-object loops, and others which have already got issues created for them. I couldn't find another open issue for this particular false positive.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions