Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

PowerShell capture PSScriptAnalyzer logs line by line into array #1863

Answered by MJVL
ugreg asked this question in Q&A
Discussion options

I am using the PSScriptAnalyzer static code checker. When I run this example commnad Invoke-ScriptAnalyzer -Path .\MySuperLongLongFileName.ps1 -Settings PSGallery the code checker logs are presented in a table format. This format causes the script name and message information to overflow into the lines below it.

RuleName Severity ScriptName Line Message
-------- -------- ---------- ---- -------
PSUseSingularNouns Warning My-Function 53 The cmdlet 'My-Function' uses a plural n
 MySuper oun. A singular noun should be used instead.
 LongLong
 FileName.
 ps1
PSUseSingularNouns Warning My-Function 84 The cmdlet 'My-Function' uses a plur
 MySuper al noun. A singular noun should be used instead.
 LongLong
 FileName.
 ps1

Ideally I want to format the output like this, so that I can parse it line by line and read the errors into any array. Any suggestions?

RuleName Severity ScriptName Line Message
-------- -------- ---------- ---- -------
PSUseSingularNouns Warning My-Function 53 MySuperLongLongFileName.ps1 The cmdlet 'My-Function' uses a plural noun. A singular noun should be used instead.
PSUseSingularNouns Warning My-Function 84 MySuperLongLongFileName.ps1 The cmdlet 'My-Function' uses a plural noun. A singular noun should be used instead.

Also raised this question on the Stack Overflow.

You must be logged in to vote

As with most other PowerShell cmdlets, Invoke-ScriptAnalyzer itself will return an object, which in this case is an array of Diagnostic Records. Assigning this output to a variable would be easier than parsing text later, as you can access the desired fields directly.

Ex:

# assign output to variable
$results = Invoke-ScriptAnalyzer -Path .\MySuperLongLongFileName.ps1 -Settings PSGallery
# show fields we can access
$results | gm
 TypeName: Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode ...

Replies: 1 comment

Comment options

As with most other PowerShell cmdlets, Invoke-ScriptAnalyzer itself will return an object, which in this case is an array of Diagnostic Records. Assigning this output to a variable would be easier than parsing text later, as you can access the desired fields directly.

Ex:

# assign output to variable
$results = Invoke-ScriptAnalyzer -Path .\MySuperLongLongFileName.ps1 -Settings PSGallery
# show fields we can access
$results | gm
 TypeName: Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
Extent Property System.Management.Automation.Language.IScriptExtent Extent {get;set;}
IsSuppressed Property bool IsSuppressed {get;set;}
Message Property string Message {get;set;}
RuleName Property string RuleName {get;set;}
RuleSuppressionID Property string RuleSuppressionID {get;set;}
ScriptName Property string ScriptName {get;}
ScriptPath Property string ScriptPath {get;set;}
Severity Property Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticSeverity Severity {get;set;}
SuggestedCorrections Property System.Collections.Generic.IEnumerable[Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.CorrectionExtent] SuggestedCorrections {get;set;}
Column ScriptProperty System.Object Column {get=$this.Extent.StartColumnNumber;}
Line ScriptProperty System.Object Line {get=$this.Extent.StartLineNumber;}
# list RuleNames
$results.RuleName
# show particular diagnostic record
$results[0]
You must be logged in to vote
0 replies
Answer selected by bergmeister
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /