Closed
Description
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
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.