Pnp-powershell: Add-PnPFile silently generating error

Created on 2 Jul 2017  路  9Comments  路  Source: pnp/PnP-PowerShell

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

  • $_.PSParentPath.Replace("SharePointPnPPowerShellOnline\SharePoint::", ...
  • ~~~~~~~~~~~~~~~~~

    • CategoryInfo : InvalidOperation: (:) [], RuntimeException

    • FullyQualifiedErrorId : InvokeMethodOnNull

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.CoreInvokeHelperTInput,TOutput
at System.Management.Automation.PowerShell.CoreInvokeTInput,TOutput
at System.Management.Automation.PowerShell.InvokeT
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.SaveStreamDataT
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?

  • [ ] 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?

(you can retrieve this by executing Get-Module -Name *pnppowershell* -ListAvailable)
2.16.1706.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

Most helpful comment

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.

All 9 comments

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?

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

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.

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

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

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.

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.

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

thanks!

Was this page helpful?
0 / 5 - 0 ratings