Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Poweshell_Formatter error when using .powershell-psscriptanalyzer.psd1 file #2229

Closed
TommyE123 opened this issue Jan 7, 2023 · 6 comments · Fixed by #2231
Closed

Poweshell_Formatter error when using .powershell-psscriptanalyzer.psd1 file #2229

TommyE123 opened this issue Jan 7, 2023 · 6 comments · Fixed by #2231
Labels
bug Something isn't working

Comments

@TommyE123
Copy link
Contributor

Describe the bug

Hi,
I believe there is a bug in the new Poweshell_Formatter linter when using the template to create your own .powershell-psscriptanalyzer.psd1 file.

To Reproduce

  • Using an Azure devops yml pipeline with a powershell script that requires linting and the default template copied to the rules folder.

  • When no .powershell-psscriptanalyzer.psd1 file is present in the rules folder then defaulting to the mega-linter template works correctly with or without errors.

  • When using your own template to use custom rules it seems to work if there’s no errors in the file or no rules that need to be used to exclude something.

  • When using your own template and there is a rule that needs to be implemented it fails with an error:

[powershell_formatter] _Pipelines/LinterRules/.powershell-psscriptanalyzer.psd1 - ERROR - 1 error(s)
--Error detail:
Invoke-Formatter: Settings file '/tmp/lint/_Pipelines/LinterRules/.powershell-psscriptanalyzer.psd1' is invalid because it does not contain a hashtable.

Expected behavior

I would expect to see Mega-Linter report the error or pass using the rule to exclude it in the config file.

Additional context

Any other information required please don’t hesitate to ask.
Thanks again for all your continued work getting this great application where it is today.
Tom

@TommyE123 TommyE123 added the bug Something isn't working label Jan 7, 2023
@nvuillam
Copy link
Member

nvuillam commented Jan 7, 2023

@TommyE123 it seems powershell does not succeed to parse your .powershell-psscriptanalyzer.psd1 config file, please could you share it here so we may see what is wrong with it ? ( cc @bdovaz )

@TommyE123
Copy link
Contributor Author

Thanks for coming back to me so quickly.

I'm using the template you have saved in MegaLinter and just changed it from PSMissingModuleManifestField to PSAvoidUsingWriteHost https://github.com/oxsecurity/megalinter/blob/main/TEMPLATES/.powershell-psscriptanalyzer.psd1

This worked fine on version v6.16.0 and the file was being used for POWERSHELL_POWERSHELL previously and hasnt been changed.

Disabling POWERSHELL_POWERSHELL_FORMATTER in the MegaLinter config then passes.
With just powershell linter and switching to just the unchanged template with a Write-Host in a powershell file fails as expected.

Unchanged template (correctly errors):
image

Changed template (correctly passes):
image

So in conclusion. I think the file is ok judging by this testing? 🙂

@nvuillam
Copy link
Member

nvuillam commented Jan 7, 2023

I think that Invoke-ScriptAnalyzer config is not valid for Invoke-Formatter

Code in MegaLinter ->

image

I have an idea to try to fix that , PR to come :)

nvuillam added a commit that referenced this issue Jan 7, 2023
* Change name of config file for powershell formatter

Fixes #2229

* Update max dev CI time to 120mn

* Add typing-extensions pip package
@nvuillam
Copy link
Member

nvuillam commented Jan 8, 2023

@TommyE123 please can you confirm it's now ok with beta version of MegaLinter ?

@TommyE123
Copy link
Contributor Author

Hi, so it’s much better now using the beta branch thank you!
However, I do still have a couple more questions and observations if I may! ☺️

  1. Although I suspect this is a very rare scenario and possibly not worth worrying about. A blank PowerShell script produces the following error. Might be worth catching the parameter is not null before performing the action?
--Error detail:
Invoke-Formatter: Cannot validate argument on parameter 'ScriptDefinition'. The argument is null. Provide a valid value for the argument, and then try running the command again.

image

  1. We are always getting the following message which I’m pretty sure is not required for a successful run as its not fixed anything!?
    • If the files have not changed, do we need to upload the files? Maybe we can check for differences and only upload changed files and then adjust the message accordingly?
    • Only pass or fail the linter if it changes anything or not?
    • If possible, I’d be tempted to highlight in another colour and change the message slightly to make it clear that they have been created by PowerShell formatter changes and are there?
    • Should we put a fixed and/or errors count in the summary for any files which are different and been fixed?
[Updated Sources Reporter] copied 3 fixed source files in folder /tmp/lint/megalinter-reports/updated_sources.
Download it from artifacts then copy-paste it in your local repo to apply linters updates

image

  1. Should there be a different template for the config for this or does it still share the .powershell-psscriptanalyzer.psd1 file?

Thanks again and sorry for all the questions and suggestions!
I'm more than happy to do any testing you need on any of the above points. ☺️
Tom

@nvuillam
Copy link
Member

nvuillam commented Jan 8, 2023

@TommyE123 I moved this conversation in a new issue as this one is solved :)

#2237

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants