pnp / PnP-PowerShell

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

Add-PnPFile silently generating error #918

Closed janegilring closed 6 years ago

janegilring commented 7 years ago

Reporting an Issue or Missing Feature

After running Add-PnPFile, the specified file is successfully uploaded to the specified Sharepoint folder, but an error is being generated silently.

Expected behavior

The specified file should be uploaded to the specifed Sharepoint folder without errors being generated.

Actual behavior

The specified file is uploaded to the specifed Sharepoint folder. However, $Error is populated with an exception after running Add-PnpFile.

Steps to reproduce behavior

1) Clear $Error $Error.Clear() 2) Verify that $Error is empty $Error 3) Run Add-PnPFile Add-PnPFile -Path C:\Temp\test.txt -Folder "folder1/subfolder" 4) Inspect $Error and notice the exception: $Error You cannot call a method on a null-valued expression. At line:1 char:1

I noticed this issue when using the cmdlet in an Azure Automation runbook. The runbook would fail to upload the generated error message and restart. After 3 re-tries the runbook goes into a Suspended mode. To workaround this issue, I added -ErrorAction -Ignore to Add-PnPFile.

The full error message from the Azure Automation job:

Terminating sandbox on saving job stream error. [subscriptionId={2dacd2c3-3487-484f-9e60-70e98912cc18}][accountId={5c716fd7-4afe-4797-9cc0-1c0087680dcf}][sandboxId={9105e5c5-d67c-4904-91f5-4b2a9182dbab}][jobId={0a5c0212-2ab8-4f23-b3b1-0c4fa72bd2d9}][streamType=Output][exception=System.Management.Automation.CmdletInvocationException: The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested. [Microsoft.SharePoint.Client.CollectionNotInitializedException: The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.] at System.Management.Automation.Runspaces.PipelineBase.Invoke(IEnumerable input) at System.Management.Automation.PowerShell.Worker.ConstructPipelineAndDoWork(Runspace rs, Boolean performSyncInvoke) at System.Management.Automation.PowerShell.Worker.CreateRunspaceIfNeededAndDoWork(Runspace rsToUse, Boolean isSync) at System.Management.Automation.PowerShell.CoreInvokeHelper[TInput,TOutput](PSDataCollection1 input, PSDataCollection1 output, PSInvocationSettings settings) at System.Management.Automation.PowerShell.CoreInvoke[TInput,TOutput](PSDataCollection1 input, PSDataCollection1 output, PSInvocationSettings settings) at System.Management.Automation.PowerShell.Invoke[T]() at Orchestrator.Runtime.OutStreamConverter.Convert(Object value) at Orchestrator.Runtime.PsScriptJobStreamsTextFormatter.PsScriptOutputStreamData.StreamText(Object streamItem) at Orchestrator.Runtime.PsScriptJobStreamsTextFormatter.GetText(Object streamRecord, Guid jobId, StreamType streamType) at Orchestrator.Runtime.PsScriptRunbook.SaveStreamRecord(Object streamRecord, Guid jobId, StreamType streamType, Boolean flushStreams) at Orchestrator.Runtime.PsScriptRunbook.SaveStreamData[T](PSDataCollection`1 dataCollection, Guid jobId, StreamType streamType, Boolean flushStreams) at Orchestrator.Runtime.PsScriptRunbook.HandleJobStreamDataAdded(Object sender, PsJobStreamDataAddedEventArgs eventArgs)

inner exception -> Microsoft.SharePoint.Client.CollectionNotInitializedException: The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested. at Microsoft.SharePoint.Client.ClientObjectCollection.d__0.MoveNext() at Microsoft.PowerShell.Commands.Internal.Format.PSObjectHelper.SmartToString(PSObject so, MshExpressionFactory expressionFactory, Int32 enumerationLimit, StringFormatError formatErrorObject) at Microsoft.PowerShell.Commands.Internal.Format.ViewGenerator.GetExpressionDisplayValue(PSObject so, Int32 enumerationLimit, MshExpression ex, FieldFormattingDirective directive, MshExpressionResult& expressionResult) at Microsoft.PowerShell.Commands.Internal.Format.ListViewGenerator.GenerateListViewEntryFromProperties(PSObject so, Int32 enumerationLimit) at Microsoft.PowerShell.Commands.Internal.Format.ListViewGenerator.GeneratePayload(PSObject so, Int32 enumerationLimit) at Microsoft.PowerShell.Commands.Internal.Format.InnerFormatShapeCommand.WritePayloadObject(PSObject so) at Microsoft.PowerShell.Commands.Internal.Format.InnerFormatShapeCommand.ProcessRecord() at System.Management.Automation.CommandProcessor.ProcessRecord() ]

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

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

(you can retrieve this by executing Get-Module -Name *pnppowershell* -ListAvailable) 2.16.1706.1

How did you install the PnP-PowerShell Cmdlets?

skaggej commented 7 years ago

Can you post an example of your Add-PnPFile working? I'm wondering...is C:\temp\test.txt a file on your local machine or stored somewhere in Azure?

janegilring commented 7 years ago

Working example: Add-PnPFile -Path C:\Temp\test.txt -Folder "folder1/subfolder" -ErrorAction Ignore

C:\temp\test.txt is located on a Windows Server 2016 machine, which is a regular Windows Server with Microsoft Monitoring Agent installed that makes it able to accept PowerShell runnbook jobs from an Azure Automation account (so called Hybrid Runbook Worker).

ReinHier commented 6 years ago

Same issue here, I'm unable to use Add-PnPFile within an Azure Automation runbook. The workaround isn't working for me. The workbook is retried 3 times and then suspended.

Stijnc commented 6 years ago

Had the same issue here. Downgrading to 2.16. something as Jan mentioned did not work.

A workaround that did fix my issue is assigning the add-pnpfile output to a variable

$dummy = Add-PnpFile -Path $file -Folder $folder see #722

ReinHier commented 6 years ago

Thanks Stijnc, I can confirm this workaround fixed my issue too #happy.

janegilring commented 6 years ago

FYI: I now see that I am in fact assigning output to $null, so the -ErrorAction Ignore workaround might have been a coincidence during troubleshooting.

erwinvanhunen commented 6 years ago

The reason this error occurs is due to the outpput formatting we do behind the scenes. I've identified the location and will commit a fix to it.

erwinvanhunen commented 6 years ago

I merged the commit and will close this issue for now. The fix will be available in the upcoming April release.

Stijnc commented 6 years ago

thanks!

LinqLover commented 3 years ago

I'm still experiencing the issue, could you reinvestigate this? Also, the file is not visible on the SharePoint website after uploading it (I have waited a few hours), but you can download it by visiting the URL manually.

Add-PnPFile -Path $localHostPath -Folder $SharePointFolder

Forwarding to Out-Null fixes the problem.