question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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'
            )
        }
    }
}

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
rjmholtcommented, Jul 7, 2020
0reactions
LaurentDardennecommented, Aug 19, 2020

@SydneyhSmith My code having evolved, it no longer causes an error via psake or on the command line. The only thing that differs on the first run is the scan time, but that’s unrelated.

First run 
--------------------------------------------------
Build Time Report
--------------------------------------------------
Name               Duration
----               --------
Init               00:00:00.033
Clean              00:00:00.073
BeforeBuild        00:00:00.001
RemoveConditionnal 00:00:00.000
BeforeStageFiles   00:00:00.001
CoreStageFiles     00:00:00.292
TestBOM            00:00:05.819
TestLocalizedData  00:00:00.000
AfterStageFiles    00:00:00.001
StageFiles         00:00:00.001
Analyze            00:00:40.536        <-------
TestBOMAfterAll    00:00:09.232
AfterBuild         00:00:00.007
Build              00:00:00.001
Total:             00:00:56.163

Next :
-----------------------------------
Build Time Report
-----------------------------------
Name               Duration
----               --------
Init               00:00:00.003
Clean              00:00:00.159
BeforeBuild        00:00:00.002
RemoveConditionnal 00:00:00.000
BeforeStageFiles   00:00:00.001
CoreStageFiles     00:00:00.137
TestBOM            00:00:07.524
TestLocalizedData  00:00:00.000
AfterStageFiles    00:00:00.001
StageFiles         00:00:00.001
Analyze            00:00:01.004      <------
TestBOMAfterAll    00:00:14.883
AfterBuild         00:00:00.004
Build              00:00:00.001
Total:             00:00:23.749

The module was not installed via Install-Module but downloaded from the PS Gallery + ‘XCopy’.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - What is a NullReferenceException, and how do I fix it?
This means the reference is null , and you cannot access members (such as methods) through a null reference. The simplest case: string...
Read more >
How can I fix the error: System.NullReferenceException
A NullReferenceException exception is thrown when you try to access a member on a type whose value is null. A NullReferenceException exception ...
Read more >
NullReferenceException Class (System)
The exception that is thrown when there is an attempt to dereference a null object reference.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found