Closed kaiaschulz closed 8 months ago
Hey @kaiaschulz,
Powershell attributes let you attach additional information to classes, functions, and properties.
PSScriptAnalyzer checks for the suppression attribute applied to ScriptBlock param block, Function param block, Class type definitions, and DSC Configurations.
Your example scripts are applying the attribute to the hashtable
itself - which doesn't seem valid in this case.
You can put a param()
block at the top of your file and apply the attribute to that.
You will also need to remove the errant space at the end of the rule name. You currently have 'PSUseLiteralInitializerForHashtable '
.
The below correctly supresses the PSUseLiteralInitializerForHashtable
rule.
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseLiteralInitializerForHashtable', '', Justification = 'PSUseLiteralInitializerForHashtable ')]
param()
$htName2 = [System.Collections.Hashtable]::Synchronized((New-Object System.Collections.Hashtable))
Hope that helps 😀!
Hi @liamjpeters,
thanks a lot for the detailed description and links for details.
After you responded with that, I found that I had some trailing whitespaces which were causing the issue:
Wrote:
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseLiteralInitializerForHashtable ', '', Justification = 'PSUseLiteralInitializerForHashtable ')]
Right:
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseLiteralInitializerForHashtable', '', Justification = 'PSUseLiteralInitializerForHashtable')]
I'm ashamed that I overlooked that. sorry
Before submitting a bug report:
Steps to reproduce
scriptName_Parallel.ps1:
scriptName.ps1
Expected behavior
I wouldn't expect that I am getting these messaged as I am trying to supress them with
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseLiteralInitializerForHashtable ', '', Justification = 'PSUseLiteralInitializerForHashtable ')]
and using the correct way of Create the hashtable using a literal hashtable expression.Actual behavior
If an unexpected error was thrown then please report the full error details using e.g.
$error[0] | Select-Object *
Environment data