Downloading SharePoint online list items' attachments locally will not allow me to open those files (seems the file are corrupted)

john john 1,026 Reputation points
2022-12-09T00:15:46.583+00:00

I have a SharePoint online list named "tickets", and this list contain items with attachments. so i want to download all the list item attachments and save them to a local folder "c:\attachments". so i wrote this PnP Power Shell script:-

  $ApprovalListItems = Get-PnPListItem -List "tickets" -PageSize 1000 -ScriptBlock { Param($items) $items.Context.ExecuteQuery()} | ForEach-Object {  
       
  $ctx = Get-PnPContext  
  $spSourceWeb= Get-PnPWeb  
      
  $ItemAttachmentURLPrefix = 'https://****.sharepoint.com/Lists/tickets/Attachments/'+$_['ID']+'/'  
  $attachmentCollection = ForEach-Object{Get-PnPProperty -ClientObject $_ -Property "AttachmentFiles"}  
      
  $AttachmentSeq = 0  
  ForEach($Attachment in $attachmentCollection)  
            {  
            Write-Host "`tDownloading Attachement: " $attachment $ItemAttachmentURLPrefix  
              
            $AttachmentSeq++  
      
            $AttachmentDataObj = "" | Select "Attachment Seq", "JDE Company", "Vendor Number", "Vendor Invoice Number", "Attachment Name", "Attachment System Name", "Job-run Date/Time Stamp"  
    Write-Host $Attachment.FileName  
            $file = Get-PnPFile -Url ($ItemAttachmentURLPrefix + $Attachment.FileName)  
            $bytes = (Get-PnPFile -Url ($ItemAttachmentURLPrefix + $Attachment.FileName)).OpenBinaryStream()  
            $name = "C:\Attachments\"+$Attachment.FileName  
             $fs = New-Object System.IO.StreamWriter($name, "OpenOrCreate")   
            $fs.Write($bytes, 0 , $bytes.Length)      
            $fs.Close()   
            $stream = $streamResult.Value  
            Write-Host $file.Name  
            Write-Host $stream.Name  
            }  
   }  

but the files that will get saved can not be opened, for example this excel sheet will raise this error:-

enter image description here

while a pdf will raise this error:-

268755-image.png

any advice what is wrong with my script?

Thanks

Although i use to have a similar script for our on-premises server and the process was working well:-

 ForEach($Attachment in $attachmentCollection)  
    {  
        Write-Host "`tDownloading Attachement: " $attachment $ItemAttachmentURLPrefix  
          
        $AttachmentSeq++  
  
        $AttachmentDataObj = "" | Select "Attachment Seq", "JDE Company", "Vendor Number", "Vendor Invoice Number", "Attachment Name", "Attachment System Name", "Job-run Date/Time Stamp"  
  
        $file = $spSourceWeb.GetFile($ItemAttachmentURLPrefix + $attachment)  
        $bytes = $file.OpenBinary()   
        $AttachmentFileName = $FilePrefix + "_SensusAP_" + $InvoiceIDFormatted + "_" + $attachment  
        $Attachmentpath = $AttachmentFolder + $AttachmentFileName #$FilePrefix + "_SensusAP_" + $InvoiceIDFormatted + "_" + $attachment  
        $fs = new-object System.IO.FileStream($Attachmentpath, "OpenOrCreate")   
        $fs.Write($bytes, 0 , $bytes.Length)      
        $fs.Close()   
Microsoft 365 and Office | SharePoint | For business | Windows
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 40,496 Reputation points Microsoft External Staff
    2022-12-09T02:21:52.94+00:00

    Hi @john john
    You can take following steps to fix this issue.
    First click on the File menu. In Info screen that appears, click on Options. Under Save workbooks section, click on the Save files in this file format: drop-down. Select a file format (i.e., the one supported by your Excel version).

    268832-image.png
    Click OK and check if you can open your Excel file. If not, try the next workaround in this article.

    https://www.stellarinfo.com/article/excel-cannot-open-the-file-because-the-extension-is-not-valid.php

    Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please make sure that you completely understand the risk before retrieving any suggestions from the above link.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.