Skip to content

AvoidReservedCharInCmdlet throws System.NullReferenceException sometimes due to CommandInfo.ResolveParameter issue #1538

Closed
@LaurentDardenne

Description

@LaurentDardenne

I report this error although I cannot reproduce the bug, moreover the analyzed code is private :-/

The exception is random during a build via PSake. I already had this error with the previous version 1.8.
The exception is most often raised when I open a powershell session and then execute my build script.
When, after the triggering of the error, I relaunch my error script, I no longer have an error

I have not yet had time to run only the PSSA command line.

Actual behavior

[STA] E:\Projets\xxxx> $error[0]|select *


PSMessageDetails      :
Exception             : System.NullReferenceException: Object reference not set to an instance of an object.
                           at System.Management.Automation.CommandInfo.ResolveParameter(String name)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.Helper.GetExportedFunction(Ast ast)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.AvoidReservedCharInCmdlet.<AnalyzeScript>d__0.MoveNext()
                           at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
                           at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.ScriptAnalyzer.<>c__DisplayClass83_1.<Analyze
                        SyntaxTree>b__2()
TargetObject          : E:\RELEASES\xxx\yyyy\Modules\Log4Posh\Log4Posh.psm1
CategoryInfo          : InvalidOperation: (E:\RELEASES\xxx…\Log4Posh.psm1:String) [Invoke-ScriptAnalyzer],
                        NullReferenceException
FullyQualifiedErrorId : RULE_ERROR,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeScriptAnalyzerCommand
ErrorDetails          :
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at <ScriptBlock>, E:\Projets\xxx\build.psake.ps1: line 154
                        at Invoke-Task, E:\...\Modules\psake\4.9.0\public\Invoke-Task.ps1: line 108
                        at Invoke-Task, E:\...\Modules\psake\4.9.0\public\Invoke-Task.ps1: line 84
                        at <ScriptBlock>, E:\...\Modules\psake\4.9.0\public\Invoke-psake.ps1: line 303
                        at ExecuteInBuildFileScope,
                        E:\...\Modules\psake\4.9.0\private\ExecuteInBuildFileScope.ps1: line 57
                        at Invoke-psake, E:\...\Modules\psake\4.9.0\public\Invoke-psake.ps1: line 258
                        at <ScriptBlock>, E:\Projets\xxx\build.ps1: line 14
                        at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo : {}


--OR

PSMessageDetails      :
Exception             : System.NullReferenceException: Object reference not set to an instance of an object.
                           at System.Management.Automation.CommandInfo.ResolveParameter(String name)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.Helper.GetExportedFunction(Ast ast)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.AvoidReservedCharInCmdlet.<AnalyzeScript>d__0.MoveNext()
                           at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
                           at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.ScriptAnalyzer.<>c__DisplayClass83_1.<Analyze
                        SyntaxTree>b__2()
TargetObject          : E:\RELEASES\XXX\Modules\YYY\YYY.psm1
CategoryInfo          : InvalidOperation: (E:\RELEASES\xxx...yyyy.psm1:String) [Invoke-ScriptAnalyzer],
                        NullReferenceException
FullyQualifiedErrorId : RULE_ERROR,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeScriptAnalyzerCommand
ErrorDetails          :
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at <ScriptBlock>, E:\Projets\xxx\build.psake.ps1: line 154
                        at Invoke-Task, E:\SYSTOOLS\XXX\Modules\psake\4.9.0\public\Invoke-Task.ps1: line 108
                        at Invoke-Task, E:\SYSTOOLS\XXX\Modules\psake\4.9.0\public\Invoke-Task.ps1: line 84
                        at <ScriptBlock>, E:\SYSTOOLS\XXX\Modules\psake\4.9.0\public\Invoke-psake.ps1: line 303
                        at ExecuteInBuildFileScope,
                        E:\SYSTOOLS\XXX\Modules\psake\4.9.0\private\ExecuteInBuildFileScope.ps1: line 57
                        at Invoke-psake, E:\SYSTOOLS\XXX\Modules\psake\4.9.0\public\Invoke-psake.ps1: line 258
                        at <ScriptBlock>, E:\Projets\xxxs\build.ps1: line 14
                        at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo : {}

The first three lines are always the same :


Exception             : System.NullReferenceException: Object reference not set to an instance of an object.
                           at System.Management.Automation.CommandInfo.ResolveParameter(String name)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.Helper.GetExportedFunction(Ast ast)

The fourth concerns a rule name which can be different :

  at Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.AvoidReservedCharInCmdlet.<AnalyzeScript>d__0.MoveNext()

The TargetObject property points to a module of the project. The module may not have been modified for several days and may not trigger an error during the previous analysis (10 minutes ago)

Environment data

v5.1  Windows server 2012

> (Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }
Get-Module

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Manifest   3.1.0.0    Microsoft.PowerShell.Management     {Add-Computer, Add-Content, Checkpoint-Computer, Clear-Con...
Manifest   3.1.0.0    Microsoft.PowerShell.Utility        {Add-Member, Add-Type, Clear-Variable, Compare-Object, Con...
Script     1.0.0.0    posh-git                            {Add-PoshGitToProfile, Expand-GitCommand, Format-GitBranch...
Script     4.9.0      psake                               {Assert, Exec, FormatTaskName, Framework, Get-PSakeScriptT...
Script     2.0.2      PSReadline                          {Get-PSReadLineKeyHandler, Get-PSReadLineOption, Remove-PS...
Script     1.19.0     psscriptanalyzer                    {Get-ScriptAnalyzerRule, Invoke-Formatter, Invoke-ScriptAn...

Test without any error, performed after the error is triggered :

[STA] E:\Projets\XXXs> $error.Clear(); cls;1..20|% {.\build.ps1}

Command line :

$analysisResult = Invoke-ScriptAnalyzer -Path $ModuleOutDir -Settings $ScriptAnalyzerSettingsPath -Recurse -Verbose:($VerbosePreference -eq 'Continue')

#Setting file 
@{
    ExcludeRules = @(
        'PSAvoidGlobalVars', #Legacy
        'PSAvoidUsingWMICmdlet', #Legacy
        'PSAvoidUsingDeprecatedManifestFields',
        'PSMissingModuleManifestField',
        'PSUseToExportFieldsInManifest',
        'PSUseUsingScopeModifierInNewRunspaces',
        'PSReviewUnusedParameter'
    )

    # Check if the scripts uses cmdlets that are compatible PS 3.0,
    Rules = @{
        PSUseCompatibleCommands = @{
            # Turns the rule on
            Enable = $true

            # Lists the PowerShell platforms we want to check compatibility with
            TargetProfiles = @(
                #PowerShell Version 3.0 Operating System Windows Server 2012
                'win-8_x64_6.2.9200.0_3.0_x64_4.0.30319.42000_framework'
            )
        }
    }
}

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