Copy-PnPFile PowerShell online but with different site collection
-
07-01-2021 - |
質問
I am writing a powershell script to copy files (documents) from a OneDrive site to a SharePoint site. They are in different site collections.
I am using this command:
Copy-PnPFile -SourceUrl $file.fieldvalues.FileRef -TargetUrl $destpath -OverwriteIfAlreadyExists -Force -ErrorVariable errors -ErrorAction SilentlyContinue
But it is giving me 404 error. I think the error is coming from the -sourceurl
parameter since they are from different site collection. I tried adding the whole url of the source file but it gave me this error:
Copy-PnPFile : Value does not fall within the expected range.
I can just load the files using $docLibrary = $ctx.Web.Lists.GetByTitle("Documents")
, but I'm not sure how to copy its contents to the SharePoint site. Is there any possible approach?
P.S: I am getting the files form the onedrive using $items = Get-PnPListItem -List Documents -PageSize 1000
.
解決
You could save the file in the local folder in computer firstly and then upload the file from the local folder to the document library in SharePoint online.
More references:
SharePoint Online : Upload recursive files to Document library using PowerShell.
SharePoint Online: Files operation with PnP Powershell.
https://malcolm1215.wordpress.com/2017/01/05/pnpfilesoperation/