如何分析PSScriptAnalyzer的输出

时间:2019-06-03 12:24:29

标签: powershell

我安装了插件(Ubuntu 18):

Install-Module -Name PSScriptAnalyzer

包装器脚本为checker.ps1

Param(
    [string]$file = "_",
    [string]$cfg = "_"
)

Import-Module PSScriptAnalyzer;
if (Test-Path $cfg) {
    $FullResult = Invoke-ScriptAnalyzer $file -Setting $cfg
} else {
    if ($GlobalPSScriptAnalyzerSettingsPath) {
        $FullResult = Invoke-ScriptAnalyzer $file -Setting $GlobalPSScriptAnalyzerSettingsPath
    } else {
        $FullResult = Invoke-ScriptAnalyzer -Path $file
    }
};

foreach ($Result in $FullResult) {
    Write-Host '::', $Result

    $Line       =   $Result.Line ;
    $Message    =   $Result.Message ;
    $RuleName   =   $Result.RuleName ;
    $Severity   =   $Result.Severity ;
    $Column     =   $Result.column ;
    $Extent     =   $Result.Extent.Text.trim() ;
    $Suggestion =   $Result.SuggestedCorrections.description ;

    if ($Extent) {
        Write-Host 'Line:$Line RuleName:$RuleName Severity:$Severity Extent:$Extent Message:$Message $Suggestion'
    } else {
        Write-Host 'Line:$Line RuleName:$RuleName Severity:$Severity Column:$Column Message:$Message $Suggestion'
    }
}

在运行PSScriptAnalyzer输出时很好:

PowerShell 6.2.1
PS /home/user/d> Invoke-scriptanalyzer ./bad.ps1

RuleName                            Severity     ScriptName Line  Message
--------                            --------     ---------- ----  -------
MissingExpressionAfterOperator      ParseError   bad.ps1    2     Missing expression after unary
                                                                  operator '++'.
UnexpectedToken                     ParseError   bad.ps1    2     Unexpected token '\\' in
                                                                  expression or statement.
MissingEndCurlyBrace                ParseError   bad.ps1    1     Missing closing '}' in statement
                                                                  block or type definition.

我希望我的包装器将上述输出转换为每行的变量。 但是失败了:

$ pwsh checker.ps1 -file bad.ps1
:: Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord
Line:$Line RuleName:$RuleName Severity:$Severity Column:$Column Message:$Message $Suggestion
:: Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord
Line:$Line RuleName:$RuleName Severity:$Severity Extent:$Extent Message:$Message $Suggestion
:: Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord
Line:$Line RuleName:$RuleName Severity:$Severity Extent:$Extent Message:$Message $Suggestion

我在做什么错了?

0 个答案:

没有答案