A few times ago I wrote a post on how to run PnP-PowerShell in your Azure DevOps build. Described method is a bit inconvenient (hello @waldekm :)), because you have to setup a code, which automatically installs PnP-PowerShell module. You should repeat it for every PowerShell script step. What if I want just put my PnP-PowerShell code in file, run it and that's it?
To make things easier, I've created a custom build\release step for Visual Studio Team Services (now Azure DevOps) called (guess how) - PnP-PowerShell. This step significantly simplifies the way you run PnP-PowerShell commands in Azure DevOps.
How to setup
You should perform a few steps in order to set up it.
- In your build click on Add Task and search for pnp. You will find PnP-PowerShell step (if haven't install it, click Get it fee and install):
- Add it to your build and configure required settings. Basically you should provide SharePoint site url, username and password. It's very good idea to store your credentials in Variables section (and hide password). That's why you should go to Variables and add your credentials there:
- Now all you need to do is just update corresponding textboxes with your credentials and SharePoint site url:
- You can use either inline scripts or put the script in the file and use File Path option. You can have multiple PnP-PowerShell steps in your build definition. In that case PnP-PowerShell module will be loaded only once (for the first task). That's it!
How it works
The task was written totally in TypeScript (today it's recommended way to create VSTS tasks). You can find the source code here at GitHub. Microsoft has a bunch of default tasks in VSTS. The source code for such tasks is also available at GitHub.
Writing PnP-PowerShell step from scratch is a difficult task, especially if you want to support all things, which are supported by default PowerShell step. That's why I use original PowerShell task as a base for PnP-PowerShell. At this point things became very simple. I've added a code, which dynamically loads PnP-PowerShell module (if hasn't loaded yet) and runs Connect-PnPOnline with credentials and SharePoint site url you provided in task configuration. As simple as that! Please enjoy it :)