Using PowerShell and PnP with Azure Automation

Image Title

posted by Khoa Quach
on Aug 08, 2018

I was once asked to complete some hefty maintenance and clean-up using PowerShell. It needed to run for a SharePoint Online environment on a specific schedule. I had gotten used to the on-premise experience where I would look for a timer job or scheduler that would perform the work for me, but I ended up finding an easy-to-use and inexpensive alternative for SharePoint Online: Azure Automation with runbook.

Let’s Get Started

In the video, I begin by presenting the various ways one can run the automation/workflow in an Office 365 environment. There are many options so we could not dedicate the video to all of them; instead, I opted to focus on just one called Azure Automation.

The video and accompanying post should not be considered a joint PowerShell review. I go rather quickly over the sample ran in the demo using PnP PowerShell (which I highly recommend looking into further if you are interested: https://github.com/SharePoint/PnP-PowerShell), but it would also work if I had used a generic PowerShell. Since I am working within the context of SharePoint Online, however, I thought I’d go ahead with PnP PowerShell.

A few notes:

  • It is a good idea to write, test, and develop your PS script using your local environment first prior to moving it to Azure Automation. Don’t get me wrong, you can absolutely test your script on Azure, but it might queue and slow down your development process.
  • You may connect to your site when running your PS script with your local environment using Connect-PnPOnline -Url ‘https://[site]’ -Credentials $cred;. Note that this command will need to change when you copy/paste it to your runbook, as Azure Automation allows you to create credentials to be stored, used, and retrieved. I will show you how to do that later in this article.

Once you have your script, it is time for the fun part!

Create the Azure Resource

Navigate to your Azure subscription portal at: https://portal.azure.com and create a new Automation resource. You will need to provide information such as name, subscription method, resource group, and location. These are not unique questions to the Automation resources; rather, consider them common steps for any resource created within the Azure portal.

Azure Automation

 

Adding the Needed Module

You will need to install the SharePoint Patterns and Practices Cmdlets for SharePoint Online in order to run the script. To do that, go to the Modules gallery and search for PnP.

Azure Automation

Install the module that is needed. In our example case, since we are dealing with a SharePoint Online instance, we will be installing the SharePointPnpPowerShellOnline module.

Set Up Credentials (If Needed)

Once installed, you will need to set up credentials. If you were to run the PowerShell script from your local environment or directly from a remote machine, you would be able to use the Windows credentials assigned to the machine. However, since the script is going to be run via the Automation resource, you will need to create credentials and update the script to use those credentials.

From the left navigation menu, click on Credentials, then Add credential. You would then provide the information as presented: name (an alias for this set of credentials), followed by a username and password.

Create the runbook

Once the credentials are set, you are ready to create your first runbook. Click on Runbooks, then Add Runbook, followed by Create a new runbook. You will then fill in required information, such as the name of the runbook and the type of runbook it is (in our example case, we are going to set the Runbook type to PowerShell).

Azure Automation

Copy and paste the PowerShell script that you have created into the editor (see screenshot below). In this case, you are using the Connect-PnpOnline -Url “<site>”. You will need to add the param option for -Credentials as well as the credentials created.Your command should look like this:

Azure Automation

Test the Runbook

If you want to give it a test run, click on the Test pane and hit Start. You will be able to see the output of the script as if you were running it on your local environment.’

Azure Automation

Let’s assume that all is working according to the example’s testing plane. Save and publish you runbook before moving on to the next step of arranging a runbook schedule.

Set Up Schedules

We want to run our example script on a regular basis. In order for you to do this as well, you can create a schedule. Simply click on Schedule, Link a schedule to your runbook, then Create a new schedule. Fill in the required information, such as the name of your schedule, as well as its start time, frequency, and expiration.

Azure Automation

 

Final Steps

Now that you have the hard work out of the way, the only thing left to do is to hit Start on the Overview page.

Azure Automation

***

Here at NIFTIT, from Office 365 consulting to SharePoint solutions, we can handle projects of any size and difficulty. We follow industry standards and best practices to build world-class solutions.  Learn more about our office 365 support here!