Skip to content

Powershell Code format (PSScriptAnalyzer) flags variable as unused #889

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

Closed
kapilmb opened this issue Jun 15, 2017 · 4 comments
Closed

Powershell Code format (PSScriptAnalyzer) flags variable as unused #889

kapilmb opened this issue Jun 15, 2017 · 4 comments
Labels
Issue-Bug A bug to squash.

Comments

@kapilmb
Copy link

kapilmb commented Jun 15, 2017

From @cryptonym64 on June 15, 2017 19:15

  • VSCode Version: Code 1.13.1 (379d2efb5539b09112c793d3d9a413017d736f89, 2017-06-14T18:21:47.485Z)
  • OS Version: Windows_NT ia32 10.0.14393
  • Extensions:
Extension Author Version
csharp ms-vscode 1.10.0
PowerShell ms-vscode 1.3.2
theme-stayfresh piotrekfracek 1.1.2
theme-cobalt0 sharksym 0.0.5

Steps to Reproduce:

  1. code this:
    $a = 0
    $b = @(1,2)
    $b | ForEach-Object {
    $a += 1
    }
    Write-Host $a

  2. Ctrl-Alt-F to format.

  3. Notice that it warns:
    file: 'untitled:Untitled-1' severity: 'Warning' message: 'The variable 'a' is assigned but never used. (PSUseDeclaredVarsMoreThanAssignments)' at: '4,5' source: 'PSScriptAnalyzer'

Copied from original issue: microsoft/vscode#28835

@Halkcyon
Copy link

Halkcyon commented Oct 19, 2017

I'm running into this issue in some of my scripts, especially when dealing with $global: scoped variables that are defined at the beginning of a script, manipulated in a function, and used at the end.

In this use-case, it's for a logging object that gets sent in an email for automation errors

@ghost
Copy link

ghost commented Dec 5, 2017

I am running into this issue in several of my scripts.
in one such case (code-excerpt) below:

If ($script:DNsThatHaveBeenACLReset -notcontains $MyDN.ToLower())
{
$script:DNsThatHaveBeenACLReset += $MyDN.ToLower()
#RunDSACLs """\$DC$MyDN"" /S"

$CommandlineParameters = """\\$DC\$MyDN"" /N "
	
$script:AccessRightsName_ACEArray["Defaults"] | ForEach-Object {$CommandlineParameters += $_}

RunDSACLs $CommandlineParameters

}

The analyzer is only flagging the instance of the variable within the ForEach-Object portion of this.

@TylerLeonhardt
Copy link
Member

TylerLeonhardt commented Dec 6, 2017

Thanks for this! I tried running Script Analyzer on its own against the original repro and I'm seeing the warning. Here's the repro:

In the file repro.ps1:

$a = 0
$b = @(1, 2)
$b | ForEach-Object {
    $a += 1
}
Write-Host $a

in a terminal:

Install-Module -Name PSScriptAnalyzer

Invoke-ScriptAnalyzer -Path path/to/repro.ps1 -Settings PSGallery -Recurse

The output:

RuleName                            Severity     ScriptName Line  Message
--------                            --------     ---------- ----  -------
PSUseDeclaredVarsMoreThanAssignment Warning      repro2.ps1 4     The variable 'a' is assigned but never used.

Let me go ahead and open this in https://github.com/PowerShell/PSScriptAnalyzer

@TylerLeonhardt
Copy link
Member

Actually, it looks like it's already open here!: PowerShell/PSScriptAnalyzer#827

Thanks again for the report - feel free to add more details and +1 that issue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Bug A bug to squash.
Projects
None yet
Development

No branches or pull requests

3 participants