Using PowerShell and PnP with Azure Automation

Khoa Quach
Jul 30, 2018 · 5 min read
Image for post
Image for post

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:

  1. 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.
  2. 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.

Image for post
Image for post

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.

Image for post
Image for post

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).

Image for post
Image for post

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:

Image for post
Image for post

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.

Image for post
Image for post

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.

Image for post
Image for post

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.

Image for post
Image for post

***

[Read more: Saying Goodbye to SharePoint Designer]

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!

NIFTIT SharePoint Blog

NIFTIT is a SharePoint design and development agency based…

Khoa Quach

Written by

CEO+Co-founder NIFTIT, Upteamist / MCP in SharePoint, Office 365/ Guest speaker/ top 5 Quora technical blogger. http://niftit.com/ and https://www.upteamist.com

NIFTIT SharePoint Blog

NIFTIT is a SharePoint design and development agency based in New York, Vietnam and Hong Kong. We build beautiful, data-driven web and mobile applications, and custom enterprise solutions. We help non-profits and business make smart decision

Khoa Quach

Written by

CEO+Co-founder NIFTIT, Upteamist / MCP in SharePoint, Office 365/ Guest speaker/ top 5 Quora technical blogger. http://niftit.com/ and https://www.upteamist.com

NIFTIT SharePoint Blog

NIFTIT is a SharePoint design and development agency based in New York, Vietnam and Hong Kong. We build beautiful, data-driven web and mobile applications, and custom enterprise solutions. We help non-profits and business make smart decision

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store