Random issues with Pnp Powershell in Azure Runbooks

%3CLINGO-SUB%20id%3D%22lingo-sub-832106%22%20slang%3D%22en-US%22%3ERandom%20issues%20with%20Pnp%20Powershell%20in%20Azure%20Runbooks%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-832106%22%20slang%3D%22en-US%22%3EHi%20everyone%2C%20we're%20facing%20random%20issues%20when%20running%20PnP%20powershell%20from%20Azure%20runbooks%20when%20several%20instances%20of%20the%20same%20runbook%20run%20concurrently.%3CBR%20%2F%3E%3CBR%20%2F%3EThe%20scenario%3CBR%20%2F%3E%3CBR%20%2F%3EWe%20have%20several%20of%20runbooks%2C%20they%20all%20use%20PnP%20Powershell%2C%20and%20each%20of%20them%20perform%20different%20actions%2C%20such%20as%20Enabling%20site%20collection%20app%20catalog%2C%20scripting%2C%20external%20sharing%2C%20creating%20a%20list%20etc.%3CBR%20%2F%3E%3CBR%20%2F%3EThose%20runbooks%20get%20triggered%20in%20two%20different%20ways%3A%3CBR%20%2F%3E%3CBR%20%2F%3E-%20one%20is%20triggered%20after%20a%20new%20site%20is%20created%2C%20through%20a%20site%20script%2C%20that%20triggers%20a%20logic%20app%20and%20from%20there%20we%20trigger%20the%20runbook%3CBR%20%2F%3E-%20the%20others%20are%20triggered%20via%20webhook%20(http%20post%20from%20a%20function%20attached%20to%20a%20queue)%3CBR%20%2F%3E%3CBR%20%2F%3EThe%20issues%3CBR%20%2F%3E%3CBR%20%2F%3EWhen%20the%20same%20runbook%20is%20triggered%20more%20than%20once%20at%20the%20same%20time%2C%20they%20fail%20in%20different%20ways%3A%3CBR%20%2F%3E-%20we%20see%20the%20logs%20being%20logged%20more%20than%20once%2C%20then%20the%20runbooks%20get%20suspended%3CBR%20%2F%3E-%20pnp%20randomly%20fails%2C%20it%20does%20not%20enable%20the%20site%20catalog%2C%20or%20enable%20scripting%2C%20etc%3CBR%20%2F%3E-%20if%20we%20put%20our%20code%20inside%20a%20try%20catch%20block%2C%20we%20use%20to%20read%20weird%20errors%20(null%20reference%2C%20invalid%20connection%20when%20using%20Connect-PnPOnline%2C%20etc)%3CBR%20%2F%3ESome%20errors%20are%3A%3CBR%20%2F%3ESet-PnPTenantSite%20%3A%20Object%20reference%20not%20set%20to%20an%20instance%20of%20an%20object.%3CBR%20%2F%3EAt%20line%3A41%20char%3A9%3CBR%20%2F%3E%3CBR%20%2F%3EConnect-PnPOnline%20%3A%20Token%20request%20failed.%3CBR%20%2F%3EAt%20line%3A31%20char%3A5%3CBR%20%2F%3E%3CBR%20%2F%3ESet-PnPTenantSite%20%3A%20No%20connection%2C%20please%20connect%20first%20with%20Connect-PnPOnline%3CBR%20%2F%3EAt%20line%3A41%20char%3A9%3CBR%20%2F%3E%3CBR%20%2F%3EAdd-PnPSiteCollectionAppCatalog%20%3A%20Object%20reference%20not%20set%20to%20an%20instance%20of%20an%20object.%3CBR%20%2F%3E%3CBR%20%2F%3EBackground%20info%3A%3CBR%20%2F%3E%3CBR%20%2F%3EThe%20runbooks%20were%20never%20executed%20at%20the%20same%20time%20against%20the%20same%20site.%3CBR%20%2F%3EWhen%20we%20trigger%20them%20concurrently%2C%20we%20only%20do%20it%20for%2010%20sites%2C%20and%20the%20runbooks%20are%20pretty%20simple%20(connect%20to%20the%20tenant%2C%20enable%20site%20collection%20app%20catalog%2C%20and%20then%20disconnect)%3CBR%20%2F%3E%3CBR%20%2F%3EAny%20ideas%3F%3CBR%20%2F%3E%3CBR%20%2F%3EThanks%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-832106%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAutomation%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Econcurrency%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPnP%20PowerShell%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Erunbook%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-837597%22%20slang%3D%22en-US%22%3ERe%3A%20Random%20issues%20with%20Pnp%20Powershell%20in%20Azure%20Runbooks%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-837597%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F385885%22%20target%3D%22_blank%22%3E%40antoniobriones%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHi%20Antonio%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI've%20had%20some%20issues%20with%20running%20PnP%20PowerShell%20in%20Azure%20Automation%20Runbooks%20before%20-%20and%20apparently%20some%20others%20have%20as%20well.%20Here's%20a%20thread%20on%20the%20topic%20that%20might%20cover%20some%20of%20your%20issues%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FSharePoint%2FPnP-PowerShell%2Fissues%2F1541%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2FSharePoint%2FPnP-PowerShell%2Fissues%2F1541%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAs%20you%20can%20see%2C%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F122%22%20target%3D%22_blank%22%3E%40Erwin%20van%20Hunen%3C%2FA%3E%26nbsp%3Bhimself%20recommends%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3ENotice%20that%20I%20added%20-ReturnConnection%20to%20Connect-PnPOnline%20and%20specified%20the%20-Connection%20parameter%20on%20Add-PnPListItem.%20Normally%20this%20is%20not%20needed%2C%20but%20in%20some%20cases%20in%20Azure%20the%20connections%20can%20be%20shared%20between%20script%20instances%2C%20causing%20conflicts.%20Using%20a%20combination%20of%20-ReturnConnection%20and%20-Connection%20on%20the%20cmdlets%20you%20can%20isolate%20the%20connection%20to%20this%20specific%20script.%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAnother%20tip%20that%20I've%20implemented%20(which%20one%20of%20the%20commenters%20mentions)%20which%20has%20solved%20most%20of%20my%20issues%20is%20to%20always%20capture%20the%20return%20value%20of%20your%20commands%20even%20if%20you%20don't%20need%20them%20in%20your%20script%2C%20for%20example%3A%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3E%24newSite%20%3D%20New-PnPSite%20-Type....%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ERather%20than%20simply%3A%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3ENew-PnPSite%20-Type...%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHope%20this%20helps%20you%20out%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Occasional Visitor
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
Highlighted

@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