PnP-PowerShell icon indicating copy to clipboard operation
PnP-PowerShell copied to clipboard

Add-PnPListItem - failing in RunBook on Azure Automation

Open chriswharton22 opened this issue 7 years ago • 27 comments

###Notice: many issues / bugs reported are actually related to the PnP Core Library which is used behind the scenes. Consider carefully where to report an issue:###

Reporting an Issue or Missing Feature

When using the cmdlet Add-PNPListItem from an Azure Automation runbook, i encounter errors where the runbook crashes out and restarts the job. This doesn't happen with other cmdlets and doesn't happen when using powershell directly on my local machine (with the same version cmdlet)

As a test, i changed the script from adding an item to adding a folder to an existing Doc Lib, the script executed without issue.

Expected behavior

Single Item to be added to a SharePoint list

Actual behavior

command runs successfully but re-starts the script creating three items in the sharepoint list, i cant seem to capture any error trapping on the issue.

Steps to reproduce behavior

Create a runbook in Azure Automation. Example code below which i am using in this instance

**Function Connect-SharePointSite($site){ $creds = Get-AutomationPSCredential -Name 'SharePointAutomationUser’ Connect-PnPOnline -Url $site -Credential $creds }

$myconnection = Connect-SharePointSite $ClientURL

Write-Output "Start"

$values = @{"Title" = 'ABCDEF'} Add-PnPListItem -List "Custom" -ContentType "Item" -Values $values

Write-Output "End"**

Which version of the PnP-PowerShell Cmdlets are you using?

  • [ ] PnP PowerShell for SharePoint 2013
  • [ ] PnP PowerShell for SharePoint 2016
  • [x ] PnP PowerShell for SharePoint Online

What is the version of the Cmdlet module you are running?

SharePointPnPPowerShellOnline 2.25.1804.1

How did you install the PnP-PowerShell Cmdlets?

  • [ ] MSI Installed downloaded from GitHub
  • [x ] Installed through the PowerShell Gallery with Install-Module
  • [ ] Other means

chriswharton22 avatar May 02 '18 16:05 chriswharton22

Try to add

Write-Output $error[0].Exception.Stacktrace

straight after Add-PnPListItem, assuming you do see the 'Start' and 'End' messages? If anything goes wrong with Add-PnPListItem it should show there.

Are you running multiple scripts at the same time? If so, consider returning the connection from your Connect-SharePointSite function:

Function Connect-SharePointSite($site){
$creds = Get-AutomationPSCredential -Name 'SharePointAutomationUser’
Connect-PnPOnline -Url $site -Credential $creds -ReturnConnection
}
$myconnection = Connect-SharePointSite $ClientURL

Write-Output "Start"

$values = @{"Title" = 'ABCDEF'}
Add-PnPListItem -List "Custom" -ContentType "Item" -Values $values -Connection $myconnection

Write-Output "End"

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.

erwinvanhunen avatar May 11 '18 14:05 erwinvanhunen

I am having the same problem... after adding 3 items the runbook restarts.

@erwinvanhunen I added your suggestions, but also that didn't help. I am even not getting any error.

Write-Output "AddItemStart"
Add-PnPListItem -List "GroupsTest" -Values @{'Unique_x0020_Identifier' = $Group.Name;'Title' = $Group.DisplayName;} -ContentType 'Item' -Connection $SPOconnection
Write-Output "AddItemFinished"

The "AddItemStart" is displayed but not "AddItemFinished".

ShinyShrimp avatar May 16 '18 04:05 ShinyShrimp

I have tried the suggestions and did not find any improvement, the items do get added as previously mentioned but then the script restarts as ShinyShrimp described.

Sent from my iPhone

On 16 May 2018, at 05:05, ShinyShrimp [email protected] wrote:

I am having the same problem... after adding 3 items the runbook restarts.

@erwinvanhunen I added your suggestions, but also that didn't help. I am even not getting any error.

Write-Output "AddItemStart" Add-PnPListItem -List "GroupsTest" -Values @{'Unique_x0020_Identifier' = $Group.Name;'Title' = $Group.DisplayName;} -ContentType 'Item' -Connection $SPOconnection Write-Output "AddItemFinished" The "AddItemStart" is displayed but not "AddItemFinished".

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

chriswharton22 avatar May 16 '18 06:05 chriswharton22

I am having the same issue... restarts 3 times when I Add-PnPListItem or Set-PnPListItem.

mbuckman10 avatar May 17 '18 14:05 mbuckman10

I am able to create an item with Add-PnPListItem without the -Values parameter and get past it. When I try to then Set-PnPListItem, it gets hung up again and restarts.

mbuckman10 avatar May 17 '18 18:05 mbuckman10

Hi,

I am also having the exact same issue using the exact same commands (connect-pnponline, set/add-pnplistitem). Do we have any new information on this issue or anything to try in the meantime?

Thanks!

jfuller28495 avatar May 31 '18 18:05 jfuller28495

I am experiencing this exact problem, but with Set-PnpListItem.

There is no error message, there is no warning, no exception to be caught. The Azure Automation runbook crashes silently, is restarted another 2 times failing again and then goes into "Suspended" state. The link provided by Microsoft says that this happens on failing to properly load DLLs or on OutOfMemoryExceptions.

Is there any update on this issue?

PowershellNinja avatar Jun 06 '18 12:06 PowershellNinja

The solution to this problem is to store the result of PNP Actions in a variable, even if you dont need the return object. I've noticed that Azure Automation doesnt like it when you just run PNP Commands without catching the return object.

So instead of this: Add-PnPListItem -blablbla

you should do this: $addedItem = Add-PnPListItem -blablbla

You should do this with every PNP operation.

jwiersem avatar Jul 03 '18 07:07 jwiersem

I see, thanks. In the end I just resolved the Issue by using plain old CSOM directly, but if this solves the issue I guess ist not a problem of PnP but rather Azure Automation that needs to improve the way they handle free-floating return objects. Thanks a lot!

PowershellNinja avatar Jul 04 '18 06:07 PowershellNinja

CAtching the return object solved the issue. Thanks

dheepai avatar Jul 19 '18 03:07 dheepai

The solution to this problem is to store the result of PNP Actions in a variable, even if you dont need the return object. I've noticed that Azure Automation doesnt like it when you just run PNP Commands without catching the return object.

So instead of this: Add-PnPListItem -blablbla

you should do this: $addedItem = Add-PnPListItem -blablbla

You should do this with every PNP operation.

This worked for me. Thanks

Joshcrypt avatar Dec 11 '18 10:12 Joshcrypt

I've just come across the same issue but with Get-PnPList. In this case, even assigning it to a variable does not work.

$credential = Get-AutomationPSCredential -Name 'RunPowerShell' Connect-PnPOnline https://.../sites/... -Credential $credential

$list = Get-PnPList Write-Output $list

Get-PnPWeb also doesn't work in the same way. The runbook gets suspended after three retries. Any idea what I could try to resolve this?

colonelclaypoo avatar Feb 22 '19 10:02 colonelclaypoo

Just noticed the error seems to be the Write-Output $list. Without this the script runs just fine.

colonelclaypoo avatar Feb 22 '19 11:02 colonelclaypoo

@colonelclaypoo It seems the issue at hand occurs when a PnP-Powershell generated object is written to the AzureAutomation output directly. The workaround I am using for all my scripts is to assign any return value to a variable and not attempt to write the whole object to the standard output. Writing single object properties to the output like list title oder web url works fine though

PowershellNinja avatar Feb 25 '19 06:02 PowershellNinja

@PowershellNinja I can confirm what you've just described. Will use the suggested workaround for the time being. Thanks for your help.

colonelclaypoo avatar Feb 25 '19 06:02 colonelclaypoo

The solution to this problem is to store the result of PNP Actions in a variable, even if you dont need the return object. I've noticed that Azure Automation doesnt like it when you just run PNP Commands without catching the return object.

So instead of this: Add-PnPListItem -blablbla

you should do this: $addedItem = Add-PnPListItem -blablbla

You should do this with every PNP operation.

This saved me so much headaches, worked like a charm. Thanks jwiersem

rgaron avatar Apr 12 '19 17:04 rgaron

If you use the variable, do you still need to use the "return connection" option or is just defining a parameter sufficient? I hav ethe same issue with the add-PNPNavigationNode, stupid enough the task was executed 3 times, so in stead of one navigation node a had 3. I am going to test your solution, but maybe the return connection you should always add, to make sure the connection is executed with the correct connection. I will let you know what my test result will be

Bart2s4 avatar May 03 '19 16:05 Bart2s4

Hi There, your idea works great, just to let you know this is i believe the best method, especially if you need to switch between sites: I have tested both, and the last one makes sure i am connected to the right site before performing the task.

Define the connection you want to execute PNP task(s) on:

$MyHubCon = Connect-PnPOnline -Credentials $cred -Url $HubSite -ReturnConnection $MySiteCon = Connect-PnPOnline -Credentials $cred -Url $Site -ReturnConnection

Execute your PnP tasks with the proper connection and store it as an object variable:

$MyJob = Add-PnP..... -bla1 -bla2 -connection $MyHubCon $MyOtherJob=Add-PnPListItem -bla1 -bla2 -bla3 -connection $MySiteCon

It works now with every connection i make now.

Bart2s4 avatar May 03 '19 17:05 Bart2s4

Just want to say that it happened to me with the Add-PnPNavigationNode cmdlet and it took a long while to target this specific line as the culprit. Finding this thread saved the day by adding a variable to gather the result. This should be added to the Azure runbook troubleshooting documentation! --Pulled requested documentation: https://github.com/MicrosoftDocs/azure-docs/pull/30776

PzKfWg avatar May 07 '19 19:05 PzKfWg

I agree PzKfWg, also good suggestion to add this to the Microsoft documentation.

Also if you need to switch between "different" Sites, I also recommend to add the -connection parameter.

Bart2s4 avatar May 08 '19 06:05 Bart2s4

Confirmed this is also an issue with the Add-PnPFile cmdlet. I had a runbook where the last line was to upload a file to Sharepoint.

The file was uploaded, but the job never completes and winds up in a suspended state. Capturing the output to a variable resolves it.

I.e. $spfile = (Add-PnpFile -Path somefile.csv -Folder $folder)

airmnichols avatar May 24 '19 13:05 airmnichols

hmm, I have a bit of a problem understanding de code, I see a variable, but then you have this in "quotes" so now it is a value, so I would first test your code locally on powershell, to see if this works. Are you running this code in Azure Automation? Because this issue is only for automation. So how are you calling the csv file?? This must then also be stored in the azure environment. I recommend first to check some examples about Add-PnPListItem command and the "for each loop". Also if you use returnconnection you should store this in a variable and then call the connection in the command, so add-pnpListItem …….. -connection $MyStoredConnection

Bart2s4 avatar Jul 22 '19 08:07 Bart2s4

$creds = Get-AutomationPSCredential -Name 'user'
$WebUrl = "SharePointsite"
Connect-PnPOnline –Url $WebUrl –Credentials $creds -ReturnConnection

Import-Csv file.csv | ForEach {

    $displayName = $_.Displayname

    $addPnPList = (Add-PnPListItem -List "sharepoint-list" -Values @{"Title" = "$displayname"})

$addPnPList

    }

I have the same problem with 3 restarts and then the job get suspended. Is there something I missed?

According to Microsoft Doc -ReturnConnection in "Connect-PnPOnline" cmdlet returns the connection and it can be used in subsequent PnP cmdlets. I would suggest you to store the connection in a variable and pass it to your "Add-PnPListItem" cmdlet. So after updating your code, Line-3 becomes: $pnpConn = Connect-PnPOnline –Url $WebUrl –Credentials $creds -ReturnConnection

and Line-6 becomes: $addPnPList = Add-PnPListItem -List "sharepoint-list" -Values @{"Title" = "$displayname"} -Connection $pnpConn

I have followed this process and it didn't break even with thousand records.

smuksud avatar Sep 18 '19 06:09 smuksud

Is this a duplicate of #918? But that has been solved, and I am still experiencing the problem.

LinqLover avatar Dec 17 '20 21:12 LinqLover

If you first "assign" a variable, you will not have an issue, so if you want to run the command Add-PnPListitem, first assign a variable to this. $variable = PnP-Command, also i recommend to use the ReturnConnection parameter for the Connect-PnPOnline.

This works without any issue

Bart2s4 avatar Dec 18 '20 09:12 Bart2s4

Yeah, I see, but suppressing the output is clearly a workaround only ...

LinqLover avatar Dec 18 '20 09:12 LinqLover

I agree, but i have even spoken with Microsoft about this issue and they say that this is the normal behavior. So this is clearly a work-a-round, but i see no other solution. Problem is that a variable is automatically converted to an array, which is used in a automation process. there is also a jobid per proces, which looks like it is not converted without defining first a new object variable

Bart2s4 avatar Dec 18 '20 09:12 Bart2s4