Skip to content

Severity-filter works unexpected with custom script-rules #1610

Closed
@fflaten

Description

@fflaten

Using the -Severity <level> or similar setting in PSScriptAnalyzerSettings.psd1 doesn't work as expected with custom rules.

Steps to reproduce

AnalyzerRules.psm1

function Measure-AlwaysFail {
    [CmdletBinding()]
    [OutputType([Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord[]])]
    Param
    (
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [System.Management.Automation.Language.CommandAst]
        $CommandAst
    )

    Process {
        $commandName = $CommandAst.GetCommandName()

        $results = @()
        try {
            $result = [Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord]@{
                'Message'              = "Always fails on commands."
                'Extent'               = $CommandAst.Extent
                'RuleName'             = $PSCmdlet.MyInvocation.InvocationName
                'Severity'             = 'Information'
                'RuleSuppressionID'    = $commandName
            }
            $results += $result
            return $results
        }
        catch {
            $PSCmdlet.ThrowTerminatingError($PSItem)
        }
    }
}

Expected behavior

PS > $commonSettings = @{
    CustomRulePath = '.\AnalyzerRules.psm1'
    ScriptDefinition = ({ Write-Host "Hello world" }.ToString())
}
PS > Invoke-ScriptAnalyzer @commonSettings

RuleName                            Severity     ScriptName Line  Message
--------                            --------     ---------- ----  -------
AnalyzerRules\Measure-AlwaysFail    Information             1     Always fails on commands.

PS > Invoke-ScriptAnalyzer @commonSettings -Severity Information

RuleName                            Severity     ScriptName Line  Message
--------                            --------     ---------- ----  -------
AnalyzerRules\Measure-AlwaysFail    Information             1     Always fails on commands.

PS > Invoke-ScriptAnalyzer @commonSettings -Severity Warning
PS > Invoke-ScriptAnalyzer @commonSettings -Severity Error
PS > Invoke-ScriptAnalyzer @commonSettings -Severity Warning, Error
PS > 

Actual behavior

PS > $commonSettings = @{
    CustomRulePath = '.\AnalyzerRules.psm1'
    ScriptDefinition = ({ Write-Host "Hello world" }.ToString())
}
PS > Invoke-ScriptAnalyzer @commonSettings

RuleName                            Severity     ScriptName Line  Message
--------                            --------     ---------- ----  -------
AnalyzerRules\Measure-AlwaysFail    Information             1     Always fails on commands.


PS > Invoke-ScriptAnalyzer @commonSettings -Severity Information
PS > Invoke-ScriptAnalyzer @commonSettings -Severity Warning

RuleName                            Severity     ScriptName Line  Message
--------                            --------     ---------- ----  -------
AnalyzerRules\Measure-AlwaysFail    Information             1     Always fails on commands.

PS > Invoke-ScriptAnalyzer @commonSettings -Severity Error
PS > Invoke-ScriptAnalyzer @commonSettings -Severity Warning, Error

RuleName                            Severity     ScriptName Line  Message
--------                            --------     ---------- ----  -------
AnalyzerRules\Measure-AlwaysFail    Information             1     Always fails on commands.

PS > 

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      7.0.3
PSEdition                      Core
GitCommitId                    7.0.3
OS                             Microsoft Windows 10.0.19041
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
> (Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }

1.19.1

Same result in PowerShell 5.1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions