PowerShell / PSScriptAnalyzer

Download ScriptAnalyzer from PowerShellGallery
https://www.powershellgallery.com/packages/PSScriptAnalyzer/
MIT License
1.87k stars 378 forks source link

Add Get-ScriptAnalyzerSetting cmdlet to generalize a way of displaying collections of rules #1740

Open sdwheeler opened 3 years ago

sdwheeler commented 3 years ago

Summary of the new feature

As a user I want to be able to discover what rules are defined in a PSD1 file, especially those that are included with PSScriptAnalyzer. I would also like to point to any PSD1 file containing rules and get the same output.

Proposed technical implementation details (optional)

Example 1 - Get the settings in the CmdletDesign PSD1 file that is included with PSScriptAnalyer

Get-ScriptAnalyzerSetting -Settings CmdletDesign

The default output should be the same as running Get-ScriptAnalyzerRule on each rule in the PSD1.

Example 2 - Get the settings in the CmdletDesign PSD1 file and show the configuration

Get-ScriptAnalyzerSetting -Settings CmdletDesign -Configuration

Adding the configuration switch should display the rule name and any custom configuration settings for rules that allow configuration.

Example 3 - Get the settings in a user provided PSD1 file

Get-ScriptAnalyzerSetting -Path /path/to/myrules.psd1

The output should be the same and the Path parameter set should also allow the Configuration parameter.

Other considerations

If the PSD1 file can contain both inclusions and exclusions, then the out needs to indicate which rules are included and which are excluded. Consider grouping by include/exclude.

Current version = 1.20.0.

bergmeister commented 2 years ago

Good idea, at the moment one only sees the setting details indirectly when using the -Verbose switch on the Invoke- cmdlets of PSSA. Question @sdwheeler : What should the returned object be of this new cmdlet? Just an object representing the content of the settings file (in a structured object) or also details such as e.g. the path to it?

sdwheeler commented 2 years ago

I think the output should be similar to Get-ScriptAnalyzerRule, but would include configured settings as well. The path is optional but might be a good idea to include in the object. That way, if you are using from a pipeline or passing an array of config files, you would know which file contains what settings. For example, a configuration file containing the AlignAssignmentStatement rule might product the following output object:

RuleName         : PSAlignAssignmentStatement
CommonName       : Align assignment statement
Description      : Line up assignment statements such that the assignment operator are aligned.
SourceType       : Builtin
SourceName       : PS
Severity         : Warning
ImplementingType : Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.AlignAssignmentStatement
Configuration    : @{Enabled=True; CheckHashtable=True}
Path             : C:\MyRules\MyConfig.psd1

Where Configuration is a nested object of settings.


Enabled CheckHashtable
------- --------------
   True           True
sdwheeler commented 2 years ago

Another thought - The output described above should be the same thing you get from Get-ScriptAnalyzerRule, but it would show what configuration is loaded in the current session. If the path is empty, then it is showing the default settings. If the path is not empty then it shows where the configuration came from.

The difference between this and the proposed Get-ScriptAnalyzerSetting cmdlet would be that Get-ScriptAnalyzerSetting is explicitly showing the contents of a specific configuration file, not the current session.