PSScriptAnalyzer: false positive 'PSUseDeclaredVarsMoreThanAssignments'
I get the message ‘The variable ‘param’ is assigned but never used.’ My script looks like this:
$param = $MyInvocation.BoundParameters
a couple of lines further down in the same script:
. "$PSScriptRoot\wrap.script.ps1 @param"
About this issue
- Original URL
- State: open
- Created 7 years ago
- Reactions: 9
- Comments: 18 (2 by maintainers)
Just reading through this issue to see if anyone’s already asked for Pester variable special casing (which they have).
I should note here that this rule will never be able to fully correctly detect when a variable isn’t used in every case. There are several ways to go behind its back because of PowerShell’s dynamic nature (dynamic meaning that the only way to know is to execute the code, which is potentially side-effectful – this is known as “undecidable”).
Dynamic scope (the way PowerShell resolves variables, as opposed to lexical scope):
How many times is outer
$xreferred to here? Depends on where Test-X is called, because the$xit references it resolved at call time, not at definition time like it would be in Python for example.Also “call time” here could mean after script execution is started:
Or even:
Unsual scoping (in some cases impossible to know)
$env:varcould be used by other processes$global:varcould be used by other runspaces$using:varshould be straightforward since it’s a local variable copy$script:varis actually easy to check, but different to local$local:varis even easierAccessing variables through the
variable:providerUsing
Get-/Set-VariableUsing either of the last two with non-constant variable names:
Variables in contexts with mangled scopes (Pester, ForEach-Object, dot-sourcing)
A few of these could be solved in easy cases where constant arguments are given, it’s just a case of “shaving the yak”. But as a static analyzer, PSScriptAnalyzer can only do so much. Ultimately this rule is supposed to be a helpful heuristic rather than an absolute.
I just noticed this bug… It appears any time a variable is assigned inside
{}resulting in a false positive being identified by PSScriptAnalyzer.OR
bump - This also returns a false positive for global variables even when they are qualified with “$global:”, if the variable is not re-used within the scope of the current block
Ex:
Same here: The variable ‘NewFileArray’ is assigned but never used. Here is the code: `# Create an array to hold the new file attributes (skipping the “Entry #” attribute since that is not stored in the AD attribute field). $NewFileArray = @() $NewFileEntry.PSObject.Properties | Where-Object {$.Name -notlike “Entry”} | ForEach-Object { # Join the name and value of each attribute, using the colon character as a delimiter. $NewFileArray += $.Name + “:” + $_.Value }
Create an ADSI connection to the Service Account defined in the Main Window, and set the custom attribute (extensionAttribute) to the Entry #.
$UpdateAccount = [ADSI]$($SvcAccount.Properties.adspath).ToString() $ADAttribute = “extensionAttribute” + $NewFileEntry.Entry
Check to see if there were any entries in the array after excluding the Entry #.
If ($NewFileArray) { # There were so join them in a contiguous string using the comma character as a delimiter, and then store that string in the custom attribute. $UpdateAccount.$ADAttribute = $NewFileArray -join “,” } Else { # There weren’t so blank the attribute because someone must have cleared them in the Edit Window. $UpdateAccount.$ADAttribute.Clear() }`
This might be covered by some of the other examples but just in case here is another sample where the
$ln1variable is being incorrectly marked as unused:1, 2, 3 | ForEach-Object -begin { $ln1=0 } -Process { '{0,6}<<:{1}' -f ++$ln1,$_ }Same here with a
Pestertest case:Test code in the latest
VS Code insiders: