Skip to content

Transient RULE_ERROR #1351

Description

@chriskuech

I wish I could provide more information, but the error is transient and therefore cannot be deterministically reproed.

PowerShell version: 6.2.1
PSScriptAnalyzer version: 1.18.3
OS Version: Ubuntu 1604

PSScriptAnalyzer is failing occasionally with the following error:

Invoke-ScriptAnalyzer : Object reference not set to an instance of an object.
At /source/.build/Assert-ValidStyle.ps1:21 char:11
+ $issues = Invoke-ScriptAnalyzer -Path "$PSScriptRoot\.." -Recurse
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (/source/.build/powershell-helpers.psm1:String) [Invoke-ScriptAnalyzer], NullReferenceException
+ FullyQualifiedErrorId : RULE_ERROR,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeScriptAnalyzerCommand

Contents of powershell-helpers.psm1:

# execute a command block and display stderr output without throwing powershell exceptions
# Taken from https://mnaoumov.wordpress.com/2015/01/11/execution-of-external-commands-in-powershell-done-right
function Invoke-Script {
    [CmdletBinding()]
    [Alias('exec')]
    param
    (
        [ScriptBlock] $ScriptBlock,
        [string] $StderrPrefix = "",
        [int[]] $AllowedExitCodes = @(0)
    )

    $backupErrorActionPreference = $script:ErrorActionPreference

    $script:ErrorActionPreference = "Continue"
    try {
        & $ScriptBlock *>&1 | ForEach-Object -Process `
        {
            if ($_ -is [System.Management.Automation.ErrorRecord]) {
                "$StderrPrefix$_"
            }
            else {
                "$_"
            }
        }

        if ($AllowedExitCodes -notcontains $LASTEXITCODE) {
            throw "Execution failed with exit code $LASTEXITCODE"
        }
    }
    finally {
        $script:ErrorActionPreference = $backupErrorActionPreference
    }
}

function Get-File {
    param(
        [string]$Uri,
        [string]$Path,
        [string]$Hash
    )

    New-Item -ItemType Directory -Path (Split-Path $path -Parent) -Force | Out-Null

    Invoke-WebRequest -Uri $uri -OutFile $path

    $actual = Get-FileHash -Path $path -Algorithm SHA256 | Select-Object -ExpandProperty Hash

    if ($actual -ne $hash) {
        throw "File $path with hash $actual doesn't match expected hash $hash"
    }
}

function Invoke-InDirectory {
    Param(
        [ValidateScript( { Test-Path $_ -PathType Container } )]
        [string]$Path,
        [ValidateNotNullOrEmpty()]
        [scriptblock]$ScriptBlock
    )

    Push-Location $Path
    try {
        &$ScriptBlock
    }
    finally {
        Pop-Location
    }
}

Export-ModuleMember -Alias * -Function *

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