Skip to content

Invoke-ScriptAnalyzer throw a NullReferenceException #602

Closed
@LaurentDardenne

Description

@LaurentDardenne

This code return a string type instead a DiagnosticRecord instance:

$ModuleFullPath='C:\temp\TestRule.psm1'

@'
Function RuleOne{         
 [CmdletBinding()]
 [OutputType([Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord[]])]

 Param(
       [Parameter(Mandatory = $true)]
       [ValidateNotNullOrEmpty()]
       [System.Management.Automation.Language.FunctionDefinitionAst]
      $FunctionDefinitionAst
 )

  process {
   try {         
    $Result=New-object System.Collections.Arraylist
    $FunctionName=$FunctionDefinitionAst.Name
    $Result.Add('[String]') > $null
#Other error case    
#     $DR = [Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord]@{"Message"  = $Messages.MeasureRequiresRunAsAdministrator; 
#                                                 "Extent"   = $assignmentAst.Extent;
#                                                 "RuleName" = $PSCmdlet.MyInvocation.InvocationName;
#                                                 "Severity" = "Information"}
#     $Result.Add($DR)
    return $result
   }
   catch
   {
      $ER= New-Object -Typename System.Management.Automation.ErrorRecord -Argumentlist $_.Exception, 
                                                                             "TestRule-$FunctionName", 
                                                                             "NotSpecified",
                                                                             $FunctionDefinitionAst
      $PSCmdlet.ThrowTerminatingError($ER) 
   }          
  }#process
}#

Export-ModuleMember -Function RuleOne
'@ > $ModuleFullPath 

$E=$T=$null
$Ast=[System.Management.Automation.Language.Parser]::ParseFile(
        $ModuleFullPath,
        [ref]$T,
        [ref]$E
      )

RuleOne $ast.Endblock.Statements[0]  
#RuleOne 
Invoke-ScriptAnalyzer -Path $ModuleFullPath -CustomRulePath  $ModuleFullPath   
#Invoke-ScriptAnalyzer : La référence d'objet n'est pas définie à une instance d'un objet.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions