PSScriptAnalyzer: Invoke-ScriptAnalyzer throw a NullReferenceException
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.
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 16 (2 by maintainers)
I agree.
I reformulate : When a custom rule emit a type other than a DiagnosticRecord instance, PSScripanalyzer throw a exception. In this case why not clearly provide the reason ? By example : “The rule ‘xxx’ must emit a DiagnosticRecord instance.”
Ah 😕 It is not indicate inside this file. And in CommunityAnalyzerRules module no rules applies this cast. I thought the custom rule received a single type of ast that indicated in the parameter.