Skip to content

Repository files navigation

Invoke-ScriptAnalyzer

This repository contains a GitHub Action that runs PSScriptAnalyzer on your code. The action analyzes PowerShell scripts using a hashtable-based settings file to customize rule selection, severity filtering, and custom rule inclusion.

Dependencies

Inputs

InputDescriptionRequiredDefault
PathThe path to the code to test.false'.'
SettingsFilePathThe path to the settings file.false.github/linters/.powershell-psscriptanalyzer.psd1
DebugEnable debug output.false'false'
VerboseEnable verbose output.false'false'
VersionSpecifies the version of the PSScriptAnalyzer module to install (NuGet range).false
PrereleaseAllow prerelease versions of the PSScriptAnalyzer module if available.false'false'
PesterVersionSpecifies the version of the Pester module to install (NuGet range).false
PesterPrereleaseAllow prerelease versions of the Pester module if available.false'false'
GitHubVersionSpecifies the version of the GitHub module to install (NuGet range).false
GitHubPrereleaseAllow prerelease versions of the GitHub module if available.false'false'
WorkingDirectoryThe working directory where the script runs.false'.'
ReportAsJsonOutput generated reports in JSON format in addition to the configured format.false'true'
Notice_ModeControls when to show notices for test completion.false'Failed'
StepSummary_EnabledControls if a GitHub step summary should be shown.false'true'
StepSummary_ShowTestOverviewControls whether to show the test overview table in the GitHub step summary.false'true'
StepSummary_ShowTestsControls which tests to show in the GitHub step summary (Full/Failed/None).false'Failed'
StepSummary_ShowConfigurationControls whether to show the configuration details in the GitHub step summary.false'false'
Run_ExcludePathDirectories or files to be excluded from the run.false
Run_ExitExit with non-zero exit code when the test run fails.false
Run_ThrowThrow an exception when test run fails.false
Run_SkipRunRuns the discovery phase but skips run.false
Run_SkipRemainingOnFailureSkips remaining tests after failure (None/Run/Container/Block).false
CodeCoverage_EnabledEnable CodeCoverage.false
CodeCoverage_OutputFormatFormat to use for code coverage report (JaCoCo/CoverageGutters/Cobertura).false
CodeCoverage_OutputPathPath relative to the current directory where code coverage report is saved.false
CodeCoverage_OutputEncodingEncoding of the output file.false
CodeCoverage_PathDirectories or files to be used for code coverage.false
CodeCoverage_ExcludeTestsExclude tests from code coverage.false
CodeCoverage_RecursePathsWill recurse through directories in the Path option.false
CodeCoverage_CoveragePercentTargetTarget percent of code coverage that you want to achieve.false
CodeCoverage_UseBreakpointsEXPERIMENTAL: Use Profiler based tracer instead of breakpoints when false.false
CodeCoverage_SingleHitBreakpointsRemove breakpoint when it is hit.false
TestResult_EnabledEnable TestResult.false
TestResult_OutputFormatFormat to use for test result report (NUnitXml/NUnit2.5/NUnit3/JUnitXml).false
TestResult_OutputPathPath relative to the current directory where test result report is saved.false
TestResult_OutputEncodingEncoding of the output file.false
TestResult_TestSuiteNameSet the name assigned to the root 'test-suite' element.falsePSScriptAnalyzer
Should_ErrorActionControls if Should throws on error. Use 'Stop' or 'Continue'.false
Debug_ShowFullErrorsShow full errors including Pester internal stack.false
Debug_WriteDebugMessagesWrite Debug messages to screen.false
Debug_WriteDebugMessagesFromWrite Debug messages from a given source.false
Debug_ShowNavigationMarkersWrite paths after every block and test, for easy navigation.false
Debug_ReturnRawResultObjectReturns unfiltered result object, for development only.false
Output_VerbosityThe verbosity of output (None/Normal/Detailed/Diagnostic).false
Output_StackTraceVerbosityThe verbosity of stacktrace output (None/FirstLine/Filtered/Full).false
Output_CIFormatThe CI format of error output (None/Auto/AzureDevops/GithubActions).false
Output_CILogLevelThe CI log level in build logs (Error/Warning).false
Output_RenderModeThe mode used to render console output (Auto/Ansi/ConsoleColor/Plaintext).false
TestDrive_EnabledEnable TestDrive.false
TestRegistry_EnabledEnable TestRegistry.false

Outputs

The action provides the following outputs:

OutputDescription
OutcomeThe outcome of the test run (success/failure)
ConclusionThe conclusion of the test run (success/failure)
ExecutedWhether tests were executed (True/False)
ResultOverall result of the test run (Passed/Failed)
FailedCountNumber of failed tests
FailedBlocksCountNumber of failed blocks
FailedContainersCountNumber of failed containers
PassedCountNumber of passed tests
SkippedCountNumber of skipped tests
InconclusiveCountNumber of inconclusive tests
NotRunCountNumber of tests not run
TotalCountTotal count of tests

How It Works

  1. Set a Path Choose a path for your code to test into the Path input. This can be a directory or a file.

  2. Configure settings file (Optional) Create a custom settings file to customize the analysis. The settings file is a hashtable that defines the rules to include, exclude, or customize. The settings file is in the format of a .psd1 file.

    Settings File Precedence:

    The action determines which settings to use in the following order:

    1. Custom Path: If you provide a SettingsFilePath input, the action uses that file.
    2. Default Action Path: If no SettingsFilePath is provided, the action looks for a settings file at: .github/linters/.powershell-psscriptanalyzer.psd1
    3. PSScriptAnalyzer Defaults: If no settings file is found in either location, the action uses the default settings from the Invoke-ScriptAnalyzer cmdlet (all built-in rules with default severity).

    Example configurations:

    # Use a custom settings file
    - uses: PSModule/Invoke-ScriptAnalyzer@v2with:
    Path: srcSettingsFilePath: config/custom-rules.psd1# Use the default action path (.github/linters/.powershell-psscriptanalyzer.psd1)
    - uses: PSModule/Invoke-ScriptAnalyzer@v2with:
    Path: src# Use PSScriptAnalyzer defaults (no settings file)
    - uses: PSModule/Invoke-ScriptAnalyzer@v2with:
    Path: srcSettingsFilePath: ''# Explicitly skip settings file

    For more info on how to create a settings file, see the Settings Documentation file.

  3. Run the Action The tests import the settings file and use Invoke-ScriptAnalyzer to analyze the code. Each rule is evaluated, and if a rule violation is found, the test will fail for that rule. Rules that are marked to be skipped (via exclusions in the settings file) are automatically skipped in the test.

    To be clear; the action follows the settings file to determine which rules to skip.

  4. View the Results The action outputs the results of the tests to both logs and step summary. If the tests pass, the actions outcome will be success. If the tests fail, the actions outcome will be failure. To make the workflow continue even if the tests fail, you can set the continue-on-error option to true. Use this built-in feature to stop the workflow from failing so that you can aggregate the status of tests across multiple jobs.

    An example of how this is done can be seen in the Action-Test workflow file.

Example Workflow

Below is an example workflow configuration using this action:

name: Analyze PowerShell Codeon: [push, pull_request]jobs:
lint:
runs-on: ubuntu-lateststeps:
- name: Checkout codeuses: actions/checkout@v2
- name: Invoke PSScriptAnalyzeruses: PSModule/Invoke-ScriptAnalyzer@v2with:
Path: srcSettingsFilePath: .github/linters/.powershell-psscriptanalyzer.psd1

References and Links

About

A GitHub Action that analyzes your PowerShell code using PSScriptAnalyzer.

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages