Using PnP PowerShell on Visual Studio Team Services (VSTS) Hosted Agent

Visual Studio Team Services (VSTS) provides great Continuous Integration (CI) and Continuous Deployment (CD) functionalities you can leverage to implement DevOps pipelines and automation with your custom developments.

If your custom solutions rely on PnP PowerShell during their build and/or deployment processes, you will need PnP PowerShell to be installed on the agent.
Unfortunately the Hosted Agents do not have PnP PowerShell installed by default.
Note: that documentation only applies to the Hosted and Hosted 2017 agents, the Linux Hosted Agent is not supported at the moment

Install PnP PowerShell

Add a first task to your build/release definition (type PowerShell). In the Type Field select Inline Script.
In the Inline Script section copy and paste that script

Install-PackageProvider -Name NuGet -Force -Scope "CurrentUser"
Install-Module SharePointPnPPowerShellOnline -Scope "CurrentUser" -Verbose -AllowClobber -Force

Note: you can also install a specific version using the -RequiredVersion parameter at line 2.
Note: you can also improve that script according to your needs as well as save it in a file you include in your repository to best fit your pipeline.
Note: that module installation task must be included once agent phase

Using PnP PowerShell

In your scripts leveraging PnP PowerShell, before calling any command related to that module, make sure you include the following line.

Import-Module SharePointPnPPowerShellOnline -Scope "Local"

Uninstalling PnP PowerShell

Note: this step is optional if you are using the VSTS Hosted Agent and is only provided to people using custom agents on which they do not want to / can not install PnP PowerShell globally
To avoid conflicts if your scripts require a specific version of PnP PowerShell, it is a good practice to cleanup after your build/release is done.
In order to do so simply add another PowerShell task and in the inline script section copy the script bellow.

get-installedmodule SharePointPnPPowerShellOnline | ? {$_.InstalledLocation -like "*"+$home+"*"} | Uninstall-Module  -Force

Note: this is a repost of a wiki page I created on the PnP PowerShell repo
Facebook Like