Random issues with Pnp Powershell in Azure Runbooks

Copper Contributor
Hi everyone, we're facing random issues when running PnP powershell from Azure runbooks when several instances of the same runbook run concurrently.

The scenario

We have several of runbooks, they all use PnP Powershell, and each of them perform different actions, such as Enabling site collection app catalog, scripting, external sharing, creating a list etc.

Those runbooks get triggered in two different ways:

- one is triggered after a new site is created, through a site script, that triggers a logic app and from there we trigger the runbook
- the others are triggered via webhook (http post from a function attached to a queue)

The issues

When the same runbook is triggered more than once at the same time, they fail in different ways:
- we see the logs being logged more than once, then the runbooks get suspended
- pnp randomly fails, it does not enable the site catalog, or enable scripting, etc
- if we put our code inside a try catch block, we use to read weird errors (null reference, invalid connection when using Connect-PnPOnline, etc)
Some errors are:
Set-PnPTenantSite : Object reference not set to an instance of an object.
At line:41 char:9

Connect-PnPOnline : Token request failed.
At line:31 char:5

Set-PnPTenantSite : No connection, please connect first with Connect-PnPOnline
At line:41 char:9

Add-PnPSiteCollectionAppCatalog : Object reference not set to an instance of an object.

Background info:

The runbooks were never executed at the same time against the same site.
When we trigger them concurrently, we only do it for 10 sites, and the runbooks are pretty simple (connect to the tenant, enable site collection app catalog, and then disconnect)

Any ideas?

Thanks
1 Reply

@antoniobriones 

 

Hi Antonio,

 

I've had some issues with running PnP PowerShell in Azure Automation Runbooks before - and apparently some others have as well. Here's a thread on the topic that might cover some of your issues:

 

https://github.com/SharePoint/PnP-PowerShell/issues/1541

 

As you can see, @Erwin van Hunen himself recommends 

 

 

 

Notice that I added -ReturnConnection to Connect-PnPOnline and specified the -Connection parameter on Add-PnPListItem. Normally this is not needed, but in some cases in Azure the connections can be shared between script instances, causing conflicts. Using a combination of -ReturnConnection and -Connection on the cmdlets you can isolate the connection to this specific script.

 

 

 

Another tip that I've implemented (which one of the commenters mentions) which has solved most of my issues is to always capture the return value of your commands even if you don't need them in your script, for example: 

 

 

 

$newSite = New-PnPSite -Type....

 

 

 

Rather than simply: 

 

 

 

New-PnPSite -Type...

 

 

 

 

Hope this helps you out