Tuesday, September 13, 2016

PowerShell Script Analyzer

"PSScriptAnalyzer is a static code checker for Windows PowerShell modules and scripts. PSScriptAnalyzer checks the quality of Windows PowerShell code by running a set of rules. The rules are based on PowerShell best practices identified by PowerShell Team and the community. It generates DiagnosticResults (errors and warnings) to inform users about potential code defects and suggests possible solutions for improvements."
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

Official SharePoint Documentation

I have recently contributed to the official SharePoint documentation for developement. Check it out here: https://docs.microsoft.com/en-us...