Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
McAndersDK opened this issue Aug 4, 2019 · 1 comment · Fixed by #1310
Closed

Comments

@McAndersDK
Copy link

McAndersDK commented Aug 4, 2019

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
@bergmeister
Copy link
Collaborator

bergmeister commented Aug 4, 2019

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