Skip to content

[BUG] #2397

@SquirrelAssassin

Description

@SquirrelAssassin

I have the below code in an azure automation runbook. The appId/secret has full control on spo tenant. The issue is the second attachment always fails to upload with the weird error.

#Function to add a list item
function new-listitem {
    Param
    (
        [parameter(Mandatory = $true)]
        [hashtable]$hashTable,
        [parameter(Mandatory = $true)]
        [string]$splistname
    )
    $listitem = Add-PnPListItem -List $splistname -Values $hashtable
    return $listitem
}


#Function to add attachments to a list item
function new-splistattachment {
    Param
    (
        [parameter(Mandatory = $true)]
        [Microsoft.SharePoint.Client.ListItem]$listItem,
        [parameter(Mandatory = $true)]
        [string]$inputCSV,
        [parameter(Mandatory = $true)]
        [string]$fileName
    )
    $stream = $null; $attachmentInfo = $null; $attchedFile = $null
    $stream = [IO.MemoryStream]::new([Text.Encoding]::ASCII.GetBytes($inputCSV))
    $attachmentInfo = New-Object -TypeName Microsoft.SharePoint.Client.AttachmentCreationInformation 
    $attachmentInfo.FileName = $fileName
    $attachmentInfo.ContentStream = $stream
    $attchedFile = $listItem.AttachmentFiles.Add($attachmentInfo)  
    Invoke-PnPQuery
    $stream.Close()
    return $attchedFile
}
#Make the hash table for the list
$listHashTable = @{"AuditType"=$($auditType); "Report_x0020_Start_x0020_Date"=$startDateListName; "Report_x0020_End_x0020_Date"=$endDateListName; "Approver"=$($Approver); "Report_x0020_Creator"=$($AdminEmail)}


#Create the new list item
$newListItem = new-listitem -hashTable $listHashTable -splistname $sharePointList

#Attach documents to the new list item
try {
#Upload a attachment
new-splistattachment -listItem ([Microsoft.SharePoint.Client.ListItem]$newListItem) -inputCSV $summaryAdminMailboxAuditLog -fileName $($endDateFileName + '_' + $(if (($summary_array_admin.count -eq 1) -or ($summary_array_admin.count -eq 0)) {'0'} else {$summary_array_admin.count -1})  + '_' + 'entries' + '_' + 'nonOwnerMailboxAccessByAdmins.csv')
}
catch{Write-Output $_}
finally {}

#Attach documents to the new list item not sure why the second one will always fail in automation so we just run it twice
try{
    #Upload a attachment
    new-splistattachment -listItem ([Microsoft.SharePoint.Client.ListItem]$newListItem) -inputCSV $summaryAdminAndDelegateMailboxAuditLog -fileName $($endDateFileName + '_' +  $(if (($summary_array_admin_and_delegate.count -eq 1) -or ($summary_array_admin_and_delegate.count -eq 0)) {'0'} else {$summary_array_admin_and_delegate.count -1}) + '_' + 'entries' + '_' + 'nonOwnerMailboxAccessByAdminsAndDelegates.csv')
}
catch{Write-Output $_}
finally{}

Expected behavior
attachments should get uploaded

Actual behavior
I get the below error
FileName :
FileNameAsPath :
ServerRelativePath :
ServerRelativeUrl :
Context : PnP.Framework.PnPClientContext
Tag :
Path : Microsoft.SharePoint.Client.ObjectPathMethod
ObjectVersion :
ServerObjectIsNull : False
TypedObject : Microsoft.SharePoint.Client.Attachment

.Invoke-PnPQuery : User cannot be found.
.At line:105 char:9
.+ Invoke-PnPQuery
.+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
. + CategoryInfo : NotSpecified: (:) [Invoke-PnPQuery], ServerException
. + FullyQualifiedErrorId : Microsoft.SharePoint.Client.ServerException,PnP.PowerShell.Commands.Base.InvokeQuery**

FileName :
FileNameAsPath :
ServerRelativePath :
ServerRelativeUrl :
Context : PnP.Framework.PnPClientContext
Tag :
Path : Microsoft.SharePoint.Client.ObjectPathMethod
ObjectVersion :
ServerObjectIsNull : False
TypedObject : Microsoft.SharePoint.Client.Attachment

Steps to reproduce behavior
Feed the above code to Azure runbook and test it with an SPO modern site.
Other symptoms
If I run the snippet on my laptop, it works just fine. Also, other pnp cmdlets work just fine with that appId/secret on runbook.
Which version of the PnP-PowerShell Cmdlets are you using?
PnP PowerShell for SharePoint Online
What is the version of the Cmdlet module you are running?
1.11.0 PnP.PowerShell
How did you install the PnP-PowerShell Cmdlets?
Through the "modules gallery" of Azure automation account

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions