Linting in PowerShell
Continuing the theme of C++ flavoured tools I miss, lint is another essential that doesn't appear to exist in the PowerShell world. Until I discovered ScriptAnalyzer which, it seems, can do pretty much the same job for .ps1 files. Here I wrote a simple loop to check all files in the same directory.
# Install-Module PSScriptAnalyzer
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
Get-ChildItem -Path $here -Filter *.ps1 -Recurse | ForEach-Object {
Invoke-ScriptAnalyzer -Path $_.FullName -IncludeDefaultRules -OutVariable Failures
If ($Failures.Count -gt 0) {
Write-Output "$($_.Name) - $($Failures.Count) rule failures"
}
}
As with all lint tools, we sometimes need to suppress errors that don't matter to our current domain.
We do this by creating a config file with ExcludeRules
@{
Severity =
@(
'Error',
'Warning'
)
ExcludeRules =
@(
'PSAvoidUsingWriteHost',
'PSUseShouldProcessForStateChangingFunctions'
)
}
and use it when we invoke.
Invoke-ScriptAnalyzer -Path $_.FullName -IncludeDefaultRules -OutVariable Failures -Setting LintSettings.psd1