Skip to content

PSUseDeclaredVarsMoreThanAssignments reported on "get-variable -include var1,var2" #1301

Closed

Description

Before submitting a bug report:

  • Make sure you are able to repro it on the latest released version
  • Perform a quick search for existing issues to check if this bug has already been reported

Steps to reproduce

$var1 = 'var1'
$var2 = 'var2'
$included = Get-Variable -Include var1, var2
write-host $included

Expected behavior

No Error

Actual behavior

throw error on both var1 and var2 variables

If an unexpected error was thrown then please report the full error details using e.g. $error[0] | Select-Object *

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      6.2.0
PSEdition                      Core
GitCommitId                    6.2.0
OS                             Microsoft Windows 10.0.17763
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
> (Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }
1.18.1
1.18.0

Activity

  1. bergmeister commented on Aug 4, 2019

    @bergmeister
    Collaborator

    Trying to reverse engineer the parameter usage of a cmdlet like Get-Variable is not trivial, therefore PSSA has implemented only a simple algorithm to understand Get-Variable foo, therefore the following would be a workaround if you don't want to suppress the rule:

    $var1 = 'var1'
    $var2 = 'var2'
    $included = (Get-Variable var1), (Get-Variable var2)
    write-host $included

    I get that this is not ideal and could be enhanced, but the concept of 'minimum viable' is very strong in these days. If you are a bit familiar with the AST, I'd be happy to give you a few pointers if you want to make a pull request for this?
    P.S. In your case, you rather want to use the -Name instead of the -Include parameter if you want to query specific variables, -Name accepts arrays as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions