Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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:

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading