Search code examples
powershellvisual-studio-codepowershell-core

Really, how do you suppress PowerShell analyzer rules


The MS docs are here: supressing rules but it really doesn't work as documented when using VS Code. I have the following at the top of my script:

# Supress some analyzer (linter) diagnostics:
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingCmdletAliases','')]

But, the analyzer itself is complaining about that statement, "Unexpected attribute". Looks like this:

error expanded

And, who wants to see all this red!

unexpanded

Interestingly, when I add a second, similar statement, like this:

# Supress some analyzer (linter) diagnostics:
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSProvideCommentHelp','')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingCmdletAliases','')]

The first statement is no longer flagged as an error, just the second one is.

But, the bottom line is my attempt to suppress the messages is not working.

I read over the .NET docs here

I even tried adding

Add-Type -Path 'C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ReferenceAssemblies\v2.0\Microsoft.TeamFoundation.Build.Workflow.dll'

As suggested in an answer to this question, but that made no difference.

I am on PS Core 7.3.7, on Windows 10 Pro with .NET 7.0.5. And VS Code 1.82.1


Solution

  • I believe your issue is that you're trying to use the decoration outside the context it is supposed to be used in, you need to use it to decorate a param block. See Suppressing Rules.

    A simple example, before suppression:

    before

    And after:

    after

    And, if I remove the param block, I get exactly the same parsing error:

    error


    For your specific use case, for suppressing the PSAvoidUsingCmdletAliases rule and following what's stated above, you can use a dummy param() at the top of your script file and it should work properly: