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

UseDeclaredVarsMoreThanAssignments should ignore $Env: #631

Closed
javydekoning opened this issue Oct 11, 2016 · 9 comments · Fixed by #958
Closed

UseDeclaredVarsMoreThanAssignments should ignore $Env: #631

javydekoning opened this issue Oct 11, 2016 · 9 comments · Fixed by #958

Comments

@javydekoning
Copy link
Contributor

Hi,

I'm using the following code in from time to time to refresh Environment/Path variables without restarting the OS/Shell:

$env:Path = [Environment]::GetEnvironmentVariable('Path','Machine')   

I think this has proper use cases in software installs and so on.

@kapilmb kapilmb added this to the 1612 milestone Dec 2, 2016
@seanchambersgray
Copy link

+1 to this - I've experienced this due to setting $env:psmodulepath for the duration of a script. This obviously doesn't get read, so triggers this rule.

@bergmeister
Copy link
Collaborator

This is already fixed in the current version 1.16.1 i.e. Invoke-ScriptAnalyzer -ScriptDefinition '$env:Path = [Environment]::GetEnvironmentVariable(''Path'',''Machine'')' does not return a warning any more. The relevant line of code that does this is here. Will therefore close unless someone can prove otherwise.

@LaurentDardenne
Copy link

A remark, not to prove the opposite but to take into account the multiple assignment:

#PSScriptAnalyzer 1.16.1     
#PS 5.1.14409.1012

Invoke-ScriptAnalyzer -ScriptDefinition '$env:Path =$Copypath= [Environment]::GetEnvironmentVariable(''Path'',''Machine'')'
#Warning for copypath

Invoke-ScriptAnalyzer -ScriptDefinition '$env:Path =$Copypath= [Environment]::GetEnvironmentVariable(''Path'',''Machine'');$copypath +="c:\temp\"'
#Warning for Path

@bergmeister
Copy link
Collaborator

bergmeister commented Mar 10, 2018

The first one looks correct to me because $Copypath is not used. The second should not produce any warnings though although if I modify it slightly then it does not give warnings:
Invoke-ScriptAnalyzer -ScriptDef inition '$env:Path =$Copypath= [Environment]::GetEnvironmentVariable(''Path'',''Machine'');get-foo $copypath'
Multiple assignments in general (not only related to env variables) sounds like a separate issue to me.

@mattpwhite
Copy link

This is still broken for me, here's a repro (does not complain outside a function):

PS C:\> cat C:\local\sa.ps1
function Show-Bug {
    $env:example = 'a'
}
PS C:\> Invoke-ScriptAnalyzer C:\local\sa.ps1

RuleName                            Severity     ScriptName Line  Message
--------                            --------     ---------- ----  -------
PSUseDeclaredVarsMoreThanAssignment Warning      sa.ps1     2     The variable 'example' is assigned but never used.
s


PS C:\> get-module psscriptanalyzer

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     1.16.1     PSScriptAnalyzer                    {Get-ScriptAnalyzerRule, Invoke-Formatter, Invoke-ScriptAnalyzer}

PS C:\> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.15063.966
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.15063.966
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

@lzybkr
Copy link
Member

lzybkr commented Mar 30, 2018

@bergmeister - Please reopen - I still see this warning in 1.16.1 when the assignment occurs in a function or scriptblock, e.g.

$x = { $env:foo = 1 }
& $x

@bergmeister
Copy link
Collaborator

bergmeister commented Mar 31, 2018

Let me summarise:

@lzybkr
Copy link
Member

lzybkr commented Mar 31, 2018

@bergmeister - Assignments to drive qualified variables should never get a warning regardless of the context. env:, function:, hklm:, etc. If #938 is related to my example, it shouldn't be.

@bergmeister
Copy link
Collaborator

@lzybkr Sorry, you're right. When I first copy pasted your example then I also saw a warning on $x, which I attributed to the other referenced issue but it turns out that it was only because one dollar sign was missing in the example.
I opened a PR with a fix that fixes all examples given in this issue.

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