Visual Studio Online (VSO) – Powershell \\ Pester \\ Invoke-ScriptAnalyzer – Hosted Build Agent

Using source control is a great first step in managing your PowerShell code. The next small step is controlling the quality of code in your repository.

I used this great blog post (https://writeabout.net/2016/01/02/run-pester-tests-in-github-with-vsts-vso-build-badge/) to get Pester running in a Visual Studio Online hosted build agent.

It only took a little more work to get Invoke-ScriptAnalyzer running by using it through pester like I showed in this blog post (http://bentaylor.work/2016/05/powershell-pester-and-invoke-scriptanalyzer/). This allows you to fail a build if your code doesn’t pass analysis.

A Visual Studio Online hosted build agent has PowerShell V4 installed and with this you don’t have access to Install-Module which would allow you to install Invoke-ScriptAnalyzer from the PowerShell gallery. Instead we have to do a small amount of work to get it running.

This involves Archiving (ZIP) the PSScriptAnalyzer PowerShell module and placing it on a publicly available web location. I use Azure Blob storage to host the PSScriptAnalyzer Module Archive with a SAS link to access the PSScriptAnalyzer Module.

Getting and Archiving The PSScriptAnalyzer Module

Making The PSScriptAnalyzer Module Archive Available To The Hosted Build Agent

I chose to use Azure Storage Explorer but you can use PowerShell or whatever your goto tool is.

Upload The PSScriptAnalyzer Module Archive

Generate a SAS Token

Which will produce a link similar to the one below which will allow access to the PSScriptAnalyzer archive from the hosted build agent.

https://bentaylorwork.blob.core.windows.net/powershell/PSScriptAnalyzer.Zip?st=2016-09-23T14%3A35%3A00Z&se=2016-11-27T15%3A35%3A00Z&sp=r&sv=2015-12-11&sr=b&sig=RzNiO7nns0%2FgtasatlL0WXPCTxeXUnqH9y1hWYwGdtA%3D

Repository Layout

Simple Build Job

The PowerShell Scripts Seen In The Repo Above

The Below Script Runs Invoke-ScriptAnalyzer Through Pester

 

The below script will load in the required PowerShell modules ( It can be used to load in any PowerShell module that runs on PowerShell version 4). It will then run any Pester tests in the the repository which include the pester tests to run Invoke-ScriptAnalyzer in the script above. It will fail the build if any tests fail.

Take note of this line the above script as it may need modifying depending on how you archive your modules and where you place the Archive online.

Build – Pass

Build – Fail

I know the above is very brief but it should be enough information to get Invoke-ScriptAnalyzer working on a Visual Studio Online hosted build agent.

About the author

Ben Taylor

Cloud and PowerShell enthusiast with a penchant for automation and CI.

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *