Scripts

How to perform PowerShell syntax check using PSScriptAnalyzer

PowerShell syntax check

PSScriptAnalyzer (PSSA) is an open-source tool that Microsoft developed. It is a static code checker for PowerShell modules and scripts. It checks the quality of the PowerShell code by running against some set of rules that checks for potential code defects in the scripts by applying a group of built-in or customized rules on the scripts being analyzed. Here is a similar article on PowerShell debugging, Ansible Syntax Checker, how to debug a PowerShell script, how to install and debug logs with the CMTrace Tool, how to perform PowerShell syntax check using PSScriptAnalyzer, and Script debugging: Difference between testing and debugging.

The default PSSA rules cover a wide variety of best practices, it checks your script, and warn you about using aliases instead of the full name of a command, using Invoke-Expression and other dangerous cmdlets, naming your functions correctly, and a whole lot more.

PowerShell syntax check using PSScriptAnalyzer

Install-Module -Name PSScriptAnalyzer -RequiredVersion 1.11.0
Install-Module -Name PSScriptAnalyzer -Scope CurrentUser
PSScriptAnalyzer usage

You will be required to accept that you are okay with the installation of the PSScriptAnalyzer from an untrusted source “PSGallery”.

Nonetheless, The PowerShell Gallery is the central repository for PowerShell content. In it, you can find useful PowerShell modules containing PowerShell commands and Desired State Configuration (DSC) resources. In addition, You can also find PowerShell scripts, some of which may contain PowerShell workflows and which outline a set of tasks and provide sequencing for those tasks. Consequently, Microsoft authors some of these packages, and others are authored by the PowerShell community.

Validate PowerShell scripts

Moreover, to verify this has been installed correctly, run the command below

Get-Module -Name PSScriptAnalyzer -ListAvailable
PowerShell script quality control

Note: Next, you will have to import the module in order to be able to use it. If you fail to import the module as shown below, you will get an error saying the command was found but the module could not be loaded.

Import-Module -Name PSScriptAnalyzer

PowerShell script testing with Invoke-ScriptAnalyzer

Furthermore, To test my script, I can run the “Invoke-ScriptAnalyzer”. The script takes a path to a directory and gets all the items that have been written more than the specified number of days ago. However, You might be able to tell that this script has no problems already, just by looking at it. I have my random script saved as “MyScript.ps1“. To test, you can download any random script from this link.

Invoke-ScriptAnalyzer -Path .\MyScript.ps1

PSSA even tells you the line of your script where the violation occurs and provides a more detailed description of the rule violation.

Note: With the PSScriptAnalyzer installed, you could run your code through the Parser and observe if it raises an error.

I hope you found this blog post helpful. Please let me know in the comment session if you have any questions.

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x