-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
119 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# PowerShell Script Analyzer | ||
|
||
Run the PSScriptAnalyzer tool | ||
|
||
## INPUT | ||
|
||
### ENV variables | ||
|
||
none | ||
|
||
### Parameters | ||
|
||
| Name | Required | Description | Default value | | ||
| :-- | :-: | :-- | :-- | | ||
| shell | | The shell (powershell or pwsh) in which the PowerShell script in this action should run | pwsh | | ||
| path | Yes | Specifies the path to the scripts or module to be analyzed. Wildcard characters are supported. | .\\ | | ||
| excludeRule | | Comma separated list of PSScriptAnalyzer rules to exclude. Wildcard characters are supported. | | | ||
| recurse | | Runs Script Analyzer on the files in the Path directory and all subdirectories recursively. | | | ||
| output | Yes | Specifies where the path for the sarif file | results.sarif | | ||
|
||
## OUTPUT | ||
|
||
### ENV variables | ||
|
||
none | ||
|
||
### OUTPUT variables | ||
|
||
none |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
param ( | ||
[string]$Path, | ||
[string[]]$ExcludeRule, | ||
[switch]$Recurse, | ||
[string]$Output | ||
) | ||
|
||
$analyzerModule = Get-Module -ListAvailable -Name PSScriptAnalyzer | ||
if ($null -eq $analyzerModule) { | ||
Install-Module -Name PSScriptAnalyzer -Force | ||
} | ||
|
||
$sarifModule = Get-Module -ListAvailable -Name ConvertToSARIF | ||
if ($null -eq $sarifModule) { | ||
Install-Module -Name ConvertToSARIF -Force | ||
} | ||
Import-Module -Name ConvertToSARIF -Force | ||
|
||
$htPSA = [ordered]@{ Path = $Path } | ||
if ($ExcludeRule) { | ||
Write-Host "Excluding rules: $ExcludeRule" | ||
$htPSA.add('ExcludeRule', $ExcludeRule) | ||
} | ||
if ($Recurse) { | ||
Write-Host "Recurse: $Recurse" | ||
$htPSA.add('Recurse', $true) | ||
} | ||
$htCTS = [ordered]@{ FilePath = $Output } | ||
|
||
$maxRetries = 3 | ||
$retryCount = 0 | ||
$success = $false | ||
|
||
Write-Output "Modules installed, now running tests." | ||
while (-not $success -and $retryCount -lt $maxRetries) { | ||
Try { | ||
Invoke-ScriptAnalyzer @htPSA -Verbose | ConvertTo-SARIF @htCTS | ||
$success = $true | ||
} Catch { | ||
Write-Host "::Error:: $_" | ||
$retryCount++ | ||
Write-Output "Retrying... ($retryCount/$maxRetries)" | ||
} | ||
} | ||
|
||
if (-not $success) { | ||
Write-Host "::Error:: Failed after $maxRetries attempts." | ||
exit 1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
name: Run PSScriptAnalyzer | ||
author: Microsoft Corporation | ||
branding: | ||
icon: "check" | ||
color: "gray-dark" | ||
inputs: | ||
shell: | ||
description: Shell in which you want to run the action (powershell or pwsh) | ||
required: false | ||
default: pwsh | ||
path: | ||
description: 'Specifies the path to the scripts or module to be analyzed. Wildcard characters are supported.' | ||
required: true | ||
default: '.\' | ||
excludeRule: | ||
description: 'Comma separated list of PSScriptAnalyzer rules to exclude. Wildcard characters are supported.' | ||
required: false | ||
recurse: | ||
description: 'Runs Script Analyzer on the files in the Path directory and all subdirectories recursively.' | ||
required: false | ||
output: | ||
description: 'Specifies where the path for the sarif file' | ||
required: true | ||
default: 'results.sarif' | ||
runs: | ||
using: "composite" | ||
steps: | ||
- name: run | ||
shell: ${{ inputs.shell }} | ||
id: RunPSScriptAnalyzer | ||
env: | ||
_path: ${{ inputs.path }} | ||
_excludeRule: ${{ inputs.excludeRule }} | ||
_recurse: ${{ inputs.recurse }} | ||
_output: ${{ inputs.output }} | ||
run: | | ||
${{ github.action_path }}/../Invoke-AlGoAction.ps1 -ActionName "RunPSScriptAnalyzer" -Action { | ||
${{ github.action_path }}/RunPSScriptAnalyzer.ps1 -path $ENV:_path -excludeRule @($ENV:_excludeRule -Replace ' ','' -Split ',') -Recurse:$($ENV:_recurse -eq 'true') -output $ENV:_output | ||
} |