pnp / PnP-PowerShell

SharePoint PnP PowerShell CmdLets
https://pnp.github.io/powershell
Other
987 stars 662 forks source link

Add-PnPListItem - failing in RunBook on Azure Automation #1541

Open chriswharton22 opened 6 years ago

chriswharton22 commented 6 years ago

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?

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

SharePointPnPPowerShellOnline 2.25.1804.1

How did you install the PnP-PowerShell Cmdlets?

erwinvanhunen commented 6 years ago

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.

ShinyShrimp commented 6 years ago

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".

chriswharton22 commented 6 years ago

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 notifications@github.com 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.

mbuckman10 commented 6 years ago

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

mbuckman10 commented 6 years ago

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.

jfuller28495 commented 6 years ago

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!

PowershellNinja commented 6 years ago

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?

jwiersem commented 6 years ago

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.

PowershellNinja commented 6 years ago

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!

dheepai commented 6 years ago

CAtching the return object solved the issue. Thanks

joshuamangi commented 5 years ago

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

colonelclaypoo commented 5 years ago

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 commented 5 years ago

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

PowershellNinja commented 5 years ago

@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

colonelclaypoo commented 5 years ago

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

rgaron commented 5 years ago

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

Bart2s4 commented 5 years ago

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 commented 5 years ago

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.

PzKfWg commented 5 years ago

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

Bart2s4 commented 5 years ago

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.

airmnichols commented 5 years ago

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)

Bart2s4 commented 5 years ago

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

smuksud commented 5 years ago
$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.

LinqLover commented 3 years ago

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

Bart2s4 commented 3 years ago

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

LinqLover commented 3 years ago

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

Bart2s4 commented 3 years ago

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