In one of my previous posts on linting PowerShell scripts, I wasn't impressed with how GitLab CI displays PSScriptAnalyzer logs. I ended my post by asking my readers if they know how we could improve the readability of logs and one reader, Jesse Chisholm, suggested I try the script below.
function Show-LintMessages()
{
$output = ""
$input|ForEach-Object -Process {
$I = $_;
$output += ($I | Select RuleName,Severity,ScriptName,Line | Format-Table | Out-String);
$output += ($I | Select Message | Format-Table | Out-String);
}
$output | Out-Host;
}
# Demonstration of function
Invoke-ScriptAnalyzer -ScriptDefinition '"b" = "b"; function eliminate-file (){}' | Show-LintMessages
After trying it, it was exactly what I was looking for! π€©
But, since I was using PSScriptAnalyzer with Docker, I didn't want to declare this function every time I called the analyzer... So I started looking for how to make it available in my Docker container. π€
After a while, I finally managed to install the function and make it available in the terminal with a PowerShell module. By adding it to the /usr/local/share/powershell/Modules
folder, the module is automatically imported and every user who connects to the Docker container has access to the Show-LintMessages
function! π€
COPY Show-LintMessages.psm1 /usr/local/share/powershell/Modules/Show-LintMessages/Show-LintMessages.psm1
In GitLab CI, I also had to pipe the output of PSScriptAnalyser to the new command.
- pwsh -Command "Invoke-ScriptAnalyzer -EnableExit -Recurse -Path . | Show-LintMessages"
Show-LintMessages
function will format the output of the previous command.Unfortunately, the message was truncated in the GitLab CI job output... π
'Select' is an alias of 'Select-Object'. Alias can introduce possible problems and make scripts hard to maintain. Pleasβ¦
RuleName Severity ScriptName Line
-------- -------- ---------- ----
PSAvoidUsingCmdletAliases Warning Show-LintMessages.psm1 6
Message
-------
'Select' is an alias of 'Select-Object'. Alias can introduce possible problems and make scripts hard to maintain. Pleasβ¦
RuleName Severity ScriptName Line
-------- -------- ---------- ----
PSAvoidUsingWriteHost Warning test.ps1 1
Message
-------
File 'test.ps1' uses Write-Host. Avoid using Write-Host because it might not work in all hosts, does not work when therβ¦
So I tweaked the function a bit, and after a few tries, I finally ended with the script below.
function Show-LintMessages() {
$input|ForEach-Object -Process {
Write-Output "--------------------------------------------------------------------------------";
$PSItem | Select-Object RuleName,Severity,ScriptName,Line | Format-Table -Wrap | Out-String -Width 80;
Write-Output " ";
$PSItem | Select-Object Message | Format-Table -Wrap | Out-String -Width 80;
Write-Output "--------------------------------------------------------------------------------";
Write-Output " ";
}
}
Show-LintMessages.psm1
file.And the output is now much better in GitLab CI! π
--------------------------------------------------------------------------------
RuleName Severity ScriptName Line
-------- -------- ---------- ----
PSAvoidUsingWriteHost Warning test.ps1 1
Message
-------
File 'test.ps1' uses Write-Host. Avoid using Write-Host because it might not
work in all hosts, does not work when there is no host, and (prior to PS 5.0)
cannot be suppressed, captured, or redirected. Instead, use Write-Output,
Write-Verbose, or Write-Information.
--------------------------------------------------------------------------------
It's more readable, isn't it? π