Using PnP PowerShell with Azure Automation Accounts Microsoft Azure, Microsoft Power Automate, Microsoft SharePoint Online Power Automate and Azure Automation Accounts

Using PnP PowerShell with 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.

Using PnP PowerShell with Azure Automation Accounts Microsoft Azure, Microsoft Power Automate, Microsoft SharePoint Online image 1

Once you have created an Automation Account …

Using PnP PowerShell with Azure Automation Accounts Microsoft Azure, Microsoft Power Automate, Microsoft SharePoint Online image

You can create your Runbooks.

Using PnP PowerShell with Azure Automation Accounts Microsoft Azure, Microsoft Power Automate, Microsoft SharePoint Online image 2

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.

Using PnP PowerShell with Azure Automation Accounts Microsoft Azure, Microsoft Power Automate, Microsoft SharePoint Online image 3

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.

Using PnP PowerShell with Azure Automation Accounts Microsoft Azure, Microsoft Power Automate, Microsoft SharePoint Online image 4

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 Accounts' module

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

Using PnP PowerShell with Azure Automation Accounts Microsoft Azure, Microsoft Power Automate, Microsoft SharePoint Online image 6

Now I can run my job.

Using PnP PowerShell with Azure Automation Accounts Microsoft Azure, Microsoft Power Automate, Microsoft SharePoint Online image 7

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.

Using PnP PowerShell with Azure Automation Accounts Microsoft Azure, Microsoft Power Automate, Microsoft SharePoint Online image 8

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.

Using PnP PowerShell with Azure Automation Accounts Microsoft Azure, Microsoft Power Automate, Microsoft SharePoint Online image 9

Get the output from an Azure Automation job

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

Using PnP PowerShell with Azure Automation Accounts Microsoft Azure, Microsoft Power Automate, Microsoft SharePoint Online image 10

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

Using PnP PowerShell with Azure Automation Accounts Microsoft Azure, Microsoft Power Automate, Microsoft SharePoint Online image 11

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

Using PnP PowerShell with Azure Automation Accounts Microsoft Azure, Microsoft Power Automate, Microsoft SharePoint Online image 12

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.

Leave a Reply

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

%d bloggers like this: