Using PnP PowerShell with Azure Automation Accounts Microsoft Azure, Microsoft Power Automate, Microsoft SharePoint Online power automate and azure automation accounts

In the past I often used Azure Functions to run PnP PowerShell from my flows in Power Automate, but there is a good alternative, Azure Automation Accounts.

Azure Automation Accounts

Azure Automation can be very similar to Azure Functions. you can run jobs and jobs can take some input and they can generate some output.

Azure automation Runbooks can be created as for example PowerShell. So there is no real difference there either.

In You scripts you can’t have any user input or output through something like Write-Host ( Write-Output however does work). So there isn’t too much of a difference there.

It all starts by creating a Automation Account.

Add automation account
Add automation account

Once you have created an Automation Account …

PVAutomationAccount
PVAutomationAccount

You can create your Runbooks.

Create a runbook within Azure Automation
Create a runbook within Azure Automation

For me however the big difference between functions and runbooks is the way Power Automate starts jobs. Before getting to that part, it is time to have a look at runbooks.

Runbooks

Runbooks come in different flavours. Poweershell is my favourite, but Python is also an option. If you really want to make development painful you can also use a graphical version of PowerShell.

Various runbook types in Azure Automation
Various runbook types in Azure Automation

As you might have noticed there are two different version of PowerShell. PowerShell and Powershell Workflow. When I work with Power Automate, I use the PowerShell Workflow type.

Example PnP Powershell Script

In my example I’m going to send a Url of a SharePoint site to my Azure Runbook and then my Powershell script will give me title of the root site. Nothing complicated, but we can do a lot more complicated stuff with this technology.

workflow Get-SPUrl
{
   [OutputType([string])]
   Param([string] $SiteUrl)
    
   Connect-PnPOnline -ClientId cb5d965a-1ccc-4e0e-8f08-d374e1e8fa46 -Url $SiteUrl -Tenant "PieterVeenstraMVP.onmicrosoft.com" -Thumbprint A88CE1075F97003AF4303B3B7F3683ABFE3B9466

   $Web = Get-PnPWeb
   $Title = $Web.Title

   Write-Output $Title

}

One of my favourite purposes of PnP powershell is to export sites as a template and apply the template to other sites. But I will look at that another day.

Create app registration

In many of my previous posts I create an app registration and then connected to my SharePoint site using a Client ID, Tenant ID and a secret. Today I’m going to do this differently. Time to use a certificate to connect to SharePoint.

using the method described in the Connect-PnPOnline documentation I created a certificate and uploaded this to the app registration that in created in Azure portal.

Create an app registration
Create an app registration

This now gives us a thumb print that we can use to connect to SharePoint with. Remember that you will also need to configure the API permission so that the app has permissions to SharePoint sites or whatever your script needs to do.

So now we have the security in place and we have the script, but what we don’t have is a runbook that will run. We will need to install the PnP PowerShell module. With Azure functions I have always found this a bit of a pain as you upload the files into the function rather than installing the module.

Installing PnP Powershell into Azure Automation Accounts

All I have to do is find the Module option in the left hand navigation.

Azure Automation Account modules
Azure Automation Account modules

Download the PnP PowerShell using

save-module -Name PnP.PowerShell -Path c:\projects\

The above will give me a folder called PnP.Powershell. This needs to be zipped and then I can upload the module files for PnP Powershell straight into my Azure Automation Accounts

Add a Module
Add a Module

Now I can run my job.

Run a job
Run a job

Running jobs in Azure might be good but I want to run this from a flow!

Running a flow to run a runbook

So my runbook is called Get-SPUrl. This will get the Title of the site for the Url that I supply to the job.

To create a job from a runbook there is an action in the Automation Accounts connector called Create Job. all the details can be selected from the drop-downs and that is all quite straight forward.

Create a job in Power Automate
Create a job in Power Automate

The parameters that have been specified in my script with the following 2 lines are being recognized by my flow.



 [OutputType([string])]
   Param([string] $SiteUrl)

Now I can run my flow but the output from the job cannot be found in the flow run yet.

No output found in my flow run
No output found in my flow run

Get the output from an Azure Automation job

To get the output, we will have to run add the Get job output action.

Get the job output from the Azure Automation runbook
Get the job output from the Azure Automation runbook

This will now give us the output from the script that ran in our job

Get job output giving us the output from the job
Get job output giving us the output from the job

The above action returned my site name and this can be used in any following actions as shown below.

Compose action using the Get job output data
Compose action using the Get job output data

Taking this further

As mentioned above creation of sites using site templates is something that I have implemented many times before. it is a very good use of PnP PowerShell, Power Automate and Azure all mixed together.

Especially when you have for example 1000s of project sites and you forgot to add that magic list to your template. Using Power automate, PnP PowerShell and Azure could be the golden idea.

Avatar for Pieter Veenstra

By Pieter Veenstra

Business Applications Microsoft MVP working as the Head of Power Platform at Vantage 365. You can contact me using contact@sharepains.com

One thought on “Using PnP PowerShell with Azure Automation Accounts”
  1. You should use the PnP.PowerShell module from the modules gallery in the automation account. Does the same trick without having to download to a local machine first

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from SharePains by Microsoft MVP Pieter Veenstra

Subscribe now to keep reading and get access to the full archive.

Continue reading