This gist shows an example of how to create custom rules with DLLs in PSScriptAnalyzer.
There are a few obstacles to surmount before you can make custom rule DLLs work in PSScriptAnalyzer:
- The latest PSScriptAnalyzer release doesn't support custom rule DLLs. Instead you need the changes in PSScriptAnalyzer #1718.
- PSScriptAnalyzer has no NuGet package, so to build against it you must clone the repo and use a project reference to the source.
- Your custom rule project will need to target either
netcoreapp3.1
ornet452
to build against PSScriptAnalyzer for now. Ideally in future we will be able to supportnetstandard2.0
to enable this scenario.
- Build the PSScriptAnalyzer project with the required changes with
./build.ps1 -All
, it will be in./out/PSScriptAnalyzer
- Also build the custom rule project with the usual
dotnet publish
- Load PSScriptAnalyzer with
Import-Module ./out/PSScriptAnalyzer
- Invoke PSScriptAnalyzer with the custom rules with:
Invoke-ScriptAnalyzer -ScriptDefinition 'gci; Get-AzVm' -CustomRulePath -CustomRulePath .\PssaCustomRules\bin\Debug\netcoreapp3.1\publish\PssaCustomRules.dll
- Build PSScriptAnalyzer as before
- Move it to
~/.vscode-insiders/extensions/ms-vscode.powershell-preview-2021.9.1/modules
(back up the original in there first) - Create a configuration file for PSScriptAnalyzer (by default the extension will pick up
PSScriptAnalyzerSettings.psd1
in the project root):@{ CustomRulePath = "C:\path\to\CustomRules\bin\Debug\netcoreapp3.1\publish\PssaCustomRules.dll" }
- Make sure there are no other versions of PSScriptAnalyzer on the module path (currently the newest is picked up, but this will soon change to only load the bundled module)
- See VSCode load and run the rule and show the result in the Problems pane, along with the suggested code change with the lightbulb: