This project provides pre-commit hooks to lint and format PowerShell code, using the PSScriptAnalyzer static code checker.
To activate the hooks, add the following lines to the repos
list in the
project's .pre-commit-config.yaml
file:
---
repos:
- repo: https://github.com/ophiosdev/pre-commit-psscriptanalyzer.git
rev: v2.0.0
hooks:
# Check PowerShell code
- id: psscriptanalyzer-check
# Format PowerShell code
- id: psscriptanalyzer-format
These hooks require PowerShell to run.
Alternatively use this to run the hooks with Docker (so no dependency on PowerShell is needed):
---
repos:
- repo: https://github.com/ophiosdev/pre-commit-psscriptanalyzer.git
rev: v2.0.0
hooks:
# Check PowerShell code
- id: psscriptanalyzer-check-docker
# Format PowerShell code
- id: psscriptanalyzer-format-docker
The psscriptanalyzer-check
hook performs checks on PowerShell code, based on
selected best practice
rules.
It supports the following options from the PSScriptAnalyzer function
Invoke-ScriptAnalyzer
:
-CustomRulePath
-ExcludeRule
-Fix
-IncludeDefaultRules
-IncludeRule
-IncludeSuppressed
-RecurseCustomRulePath
-SaveDscDependency
-Settings
-Severity
The psscriptanalyzer-check-docker
hook supports the same options.
To pass multiple values like to the -ExcludeRule
parameter, create a
single string where the values are separated by a ,
(comma) without
any whitespaces
---
repos:
- repo: https://github.com/ophiosdev/pre-commit-psscriptanalyzer.git
rev: v2.0.0
hooks:
# Check PowerShell code
- id: psscriptanalyzer-check
args:
- '-ExcludeRule'
- 'PSAvoidUsingConvertToSecureStringWithPlainText,PSAvoidUsingWriteHost'
The psscriptanalyzer-format
formats PowerShell code to a canonical format. It
supports the following options from the PSScriptAnalyzer function
Invoke-Formatter
:
The psscriptanalyzer-format-docker
hook supports the same options.
© 2023 Mohamed El Morabity © 2024 Ophios GmbH
Licensed under the GNU GPL, version 3.0 or later.