Ref: https://github.com/PowerShell/PSScriptAnalyzer
Lets put this to the test. I have written following basic PowerShell Script in Visual Studio Code (because it is an awesome tool and it has a PowerShell extension that give you features like IntelliSense, Goto definition, Debugging etc) and will be running the Script Analyzer on the script file. In this script I am calling a function that will display a string stored in a variable.
Open the Windows PowerShell Console and lets install the PSScriptAnalyzer module (This module is available in v 5.0 and above by default):
Install-Module -Name PSScriptAnalyzer -Force
Verify if the commands are available for this module:
Get-Command -Module PSScriptAnalyzer
Run the Script Analyzer on our test script:
Invoke-ScriptAnalyzer -Path C:\Users\user1\Desktop\Test.ps1
Now the ScriptAnalyzer is showing me one warning in my most simplest script that Write-Host (which I have been using my whole life) is obsolete and use Write-Output instead.....which is awesome :o). And that's how easily we can do static analysis based on the built-in rules on our PowerSell scripts.
To get the complete list of rules, use the following command:
Get-ScriptAnalyzerRule
No comments:
Post a Comment