PowerShell / PowerShell/PSScriptAnalyzer

Suppressing PSAvoidGlobalVars for a single variable

Open
#822 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Area - Rules Issue - Enhancement
Dominant language
C#
Stars
2.2k
Forks
414
Avg merge
13h 1m
Merged PRs (30d)
2

Description

Hi, I've got a legacy script which I've started running through PSScriptAnalyzer, and I've got a bunch of global variables which I'm gradually refactoring away. However, one of them is proving difficult to deal with so I'd like to suppress the PSAvoidGlobalVars error on just that one for now, while leaving the others still erroring to remind me I need to come back and fix them later.

I don't really want to exclude the rule entirely, so I've tried this...

myScript.ps1

$global:myGlobalVar1 = "abc";

[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidGlobalVars", "")]
$global:myGlobalVar2 = "pqr";

$global:myGlobalVar3 = "xyz";

... but it doesn't seem to suppress the error for $global:myGlobalVar2.

Is there another way to do this?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the suppression example in myScript.ps1 and investigate how the PSAvoidGlobalVars rule handles SuppressMessageAttribute. Review the project's documented suppression behavior and related tests, if available. Done means establishing whether only $global:myGlobalVar2 can be suppressed and documenting or validating the supported approach without disabling the rule globally.

Written by the indexing model from the issue text.

Assessment

Tech stack
powershell
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.