Skip to content

PsScriptAnalyzer Falsely Recognizes Global Variable As Assigned But Not Used #1057

Closed
@avelis26

Description

@avelis26

VSCode Version: 1.17.0
OS Edition: Windows 10 Pro
OS Version: 1703
OS Build: 15063.674
Ext Version: 1.4.3

1.17.2
b813d12980308015bcd2b3a2f6efa5c810c33ba5

Major  Minor  Build  Revision
-----  -----  -----  --------
1      4      1      0
ms-vscode.PowerShell@1.4.3
robertohuertasm.vscode-icons@7.15.0

Key   : PSVersion
Value : 5.1.15063.674
Name  : PSVersion

Key   : PSEdition
Value : Desktop
Name  : PSEdition

Key   : PSCompatibleVersions
Value : {1.0, 2.0, 3.0, 4.0...}
Name  : PSCompatibleVersions

Key   : BuildVersion
Value : 10.0.15063.674
Name  : BuildVersion

Key   : CLRVersion
Value : 4.0.30319.42000
Name  : CLRVersion

Key   : WSManStackVersion
Value : 3.0
Name  : WSManStackVersion

Key   : PSRemotingProtocolVersion
Value : 2.3
Name  : PSRemotingProtocolVersion

Key   : SerializationVersion
Value : 1.1.0.1
Name  : SerializationVersion

Issue Description

PsScriptAnalyzer Falsely Recognizes Global Variable As Assigned But Not Used
Obviously not a critical issue but it is kind of annoying having that green squiggly line under a variable that WILL be used.

Repro Steps

  1. Open VS Code and create a PowerShell file (PowerShell extension installed obviously).
  2. Paste the below example code.
  3. Note that $global:eventType is underlined as a 'PSUseDeclaredVarsMoreThanAssignments' error.
function Write-ErrorDetails {
	$global:eventType = 'Error'
	Write-Error -Message 'An error happened.' -ErrorId $statusCode
}
try {
	Write-Output 'hello world'
	$statusCode = 0
	throw 'eventType variable is totally going to get used now!'
}
catch {
	$statusCode = 1
	Write-ErrorDetails
}
finally {
	Write-EventLog -LogName 'Applicatoin' -Source 'MyScript' -EntryType $global:eventType
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions