SharePoint Online: คัดลอกไฟล์ระหว่างไซต์คอลเลกชันโดยใช้ PowerShell
ข้อกำหนด: คัดลอกไฟล์ระหว่างไลบรารีเอกสารใน SharePoint Online Site Collections
จะคัดลอกไฟล์ระหว่างคอลเลกชันไซต์ SharePoint Online ได้อย่างไร
คุณกำลังมองหาวิธีคัดลอกไฟล์ระหว่างไซต์คอลเลกชันใน SharePoint Online หรือไม่? ซึ่งสามารถทำได้โดยใช้วิธีการต่างๆ สองสามวิธี ทั้งนี้ขึ้นอยู่กับสถานการณ์ โพสต์ในบล็อกนี้จะให้คำแนะนำทีละขั้นตอนในการคัดลอกไฟล์จากไซต์หนึ่งไปยังอีกไซต์หนึ่งด้วยตนเอง นอกจากนี้เรายังจะแสดงวิธีคัดลอกไฟล์ระหว่างไซต์คอลเลกชันโดยใช้ PowerShell เพื่อทำให้กระบวนการเป็นแบบอัตโนมัติ
ต่อไปนี้เป็นวิธีคัดลอกไฟล์ระหว่างไลบรารีเอกสารใน SharePoint Online:
นำทางไปยังไลบรารีเอกสาร SharePoint Online >> เลือกไฟล์ที่จะคัดลอก >> คลิก "คัดลอกไปที่" ในแถบเครื่องมือ (หรือคลิกขวาที่เอกสารแล้วเลือก "คัดลอกไปที่" จากเมนูบริบท)
-
ซึ่งจะเปิดแผงข้อมูลทางด้านขวา เลือกไลบรารีเป้าหมายที่คุณต้องการคัดลอกไฟล์ของคุณ คุณสามารถเลือกโฟลเดอร์ใดก็ได้ในไลบรารีปัจจุบัน ไลบรารีอื่น ๆ ในไซต์ปัจจุบัน หรือแม้แต่ไลบรารีในไซต์คอลเลกชันต่างๆ
- เลือกตำแหน่งเป้าหมายเพื่อคัดลอกเอกสารและคลิกที่ปุ่ม "คัดลอกที่นี่" เพื่อเริ่มการคัดลอกไฟล์
คุณจะเห็นข้อความ "กำลังคัดลอก" ในแถบเครื่องมือ และไฟล์ของคุณจะถูกคัดลอกในไม่ช้า คุณสามารถใช้วิธีนี้เพื่อคัดลอกไฟล์ระหว่างไลบรารีบนไซต์เดียวกันหรือระหว่างไซต์คอลเลกชันที่แตกต่างกัน
PowerShell เพื่อคัดลอกไฟล์ระหว่างไซต์คอลเลกชันใน SharePoint Online:
PowerShell เป็นวิธีที่มีประสิทธิภาพในการคัดลอกไฟล์ SharePoint Online หากคุณต้องการถ่ายโอนไฟล์ระหว่างไซต์คอลเลกชันต่างๆ อย่างรวดเร็ว หากต้องการคัดลอกไฟล์โดยใช้ PowerShell คุณจะต้องเชื่อมต่อกับทั้งไซต์ต้นทางและปลายทางก่อน เมื่อคุณเชื่อมต่อกับทั้งสองไซต์แล้ว คุณสามารถใช้วิธี CSOM OpenBinaryDirect และ SaveBinaryDirect เพื่อคัดลอกไฟล์จากไซต์ต้นทางไปยังไซต์ปลายทางได้
มาคัดลอกไฟล์เดียวจากต้นทางไปยังปลายทาง:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#Set parameter values
$SourceSiteURL="https://crescent.sharePoint.com/sites/sales"
$TargetSiteURL="https://crescent.sharepoint.com/sites/Ops/"
#Set Source and Destination File URLs - Relative path
$SourceFileURL="/Sites/Sales/Project Documents/Active Users.xlsx"
$TargetFileURL="/Sites/Ops/Shared Documents/Active Users.xlsx"
#Setup Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the contexts
$SourceCtx = New-Object Microsoft.SharePoint.Client.ClientContext($SourceSiteURL)
$SourceCtx.Credentials = $Credentials
$TargetCtx = New-Object Microsoft.SharePoint.Client.ClientContext($TargetSiteURL)
$TargetCtx.Credentials = $Credentials
#Get the Source File
$FileInfo = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($SourceCtx, $SourceFileURL)
#Copy File to the Target location
[Microsoft.SharePoint.Client.File]::SaveBinaryDirect($TargetCtx, $TargetFileURL, $FileInfo.Stream,$True)
สคริปต์นี้เพียงแค่คัดลอกไฟล์เฉพาะจากไซต์คอลเลกชัน/ไซต์ต้นทางไปยังไซต์/ไซต์คอลเลกชันเป้าหมาย
คัดลอกไฟล์และโฟลเดอร์ทั้งหมดจากไลบรารีหนึ่งไปยังไลบรารีอื่นระหว่างไซต์ต่างๆ:
PowerShell เป็นภาษาสคริปต์ที่มีประสิทธิภาพซึ่งสามารถใช้เพื่อทำงานหลายอย่างโดยอัตโนมัติ รวมถึงการคัดลอกไฟล์ระหว่างไซต์ SharePoint ต่อไปนี้เป็นวิธีคัดลอกไฟล์และโฟลเดอร์ทั้งหมดจากไลบรารีหนึ่งไปยังอีกไลบรารีหนึ่งใน SharePoint Online:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Function Copy-Files
{
param
(
[Parameter(Mandatory=$true)] [Microsoft.SharePoint.Client.Folder] $SourceFolder,
[Parameter(Mandatory=$true)] [Microsoft.SharePoint.Client.Folder] $TargetFolder
)
Try {
#Get all Files from the source folder
$SourceFilesColl = $SourceFolder.Files
$SourceFolder.Context.Load($SourceFilesColl)
$SourceFolder.Context.ExecuteQuery()
#Iterate through each file and copy
Foreach($SourceFile in $SourceFilesColl)
{
#Get the source file
$FileInfo = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($SourceFolder.Context, $SourceFile.ServerRelativeUrl)
#Copy File to the Target location
$TargetFileURL = $TargetFolder.ServerRelativeUrl+"/"+$SourceFile.Name
[Microsoft.SharePoint.Client.File]::SaveBinaryDirect($TargetFolder.Context, $TargetFileURL, $FileInfo.Stream,$True)
Write-host -f Green "Copied File '$($SourceFile.ServerRelativeUrl)' to '$TargetFileURL'"
}
#Process Sub Folders
$SubFolders = $SourceFolder.Folders
$SourceFolder.Context.Load($SubFolders)
$SourceFolder.Context.ExecuteQuery()
Foreach($SubFolder in $SubFolders)
{
If($SubFolder.Name -ne "Forms")
{
#Prepare Target Folder
$TargetFolderURL = $SubFolder.ServerRelativeUrl -replace $SourceLibrary.RootFolder.ServerRelativeUrl, $TargetLibrary.RootFolder.ServerRelativeUrl
Try {
$Folder=$TargetFolder.Context.web.GetFolderByServerRelativeUrl($TargetFolderURL)
$TargetFolder.Context.load($Folder)
$TargetFolder.Context.ExecuteQuery()
}
catch {
#Create Folder
if(!$Folder.Exists)
{
$TargetFolderURL
$Folder=$TargetFolder.Context.web.Folders.Add($TargetFolderURL)
$TargetFolder.Context.Load($Folder)
$TargetFolder.Context.ExecuteQuery()
Write-host "Folder Added:"$SubFolder.Name -f Yellow
}
}
#Call the function recursively
Copy-Files -SourceFolder $SubFolder -TargetFolder $Folder
}
}
}
Catch {
write-host -f Red "Error Copying File!" $_.Exception.Message
}
}
#Set Parameter values
$SourceSiteURL="https://crescent.sharePoint.com/sites/sales"
$TargetSiteURL="https://crescent.sharepoint.com/sites/ops"
$SourceLibraryName="Project Documents"
$TargetLibraryName="Documents"
#Setup Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the contexts
$SourceCtx = New-Object Microsoft.SharePoint.Client.ClientContext($SourceSiteURL)
$SourceCtx.Credentials = $Credentials
$TargetCtx = New-Object Microsoft.SharePoint.Client.ClientContext($TargetSiteURL)
$TargetCtx.Credentials = $Credentials
#Get the source library and Target Libraries
$SourceLibrary = $SourceCtx.Web.Lists.GetByTitle($SourceLibraryName)
$SourceCtx.Load($SourceLibrary)
$SourceCtx.Load($SourceLibrary.RootFolder)
$TargetLibrary = $TargetCtx.Web.Lists.GetByTitle($TargetLibraryName)
$TargetCtx.Load($TargetLibrary)
$TargetCtx.Load($TargetLibrary.RootFolder)
$TargetCtx.ExecuteQuery()
#Call the function
Copy-Files -SourceFolder $SourceLibrary.RootFolder -TargetFolder $TargetLibrary.RootFolder
เป็นที่น่าสังเกตว่าการคัดลอกไฟล์จะไม่รักษาประวัติเวอร์ชันไว้! คุณต้องใช้ "ย้าย" แทนการคัดลอกหากคุณต้องการคงเวอร์ชันไว้
คัดลอกไฟล์ทั้งหมดจากไลบรารีหนึ่งไปยังอีกไลบรารีหนึ่งพร้อมกับค่าข้อมูลเมตา:
สคริปต์นี้จะคัดลอกไฟล์และโฟลเดอร์ทั้งหมดระหว่างไลบรารีเอกสารในไซต์คอลเลกชันต่างๆ โดยคงค่าฟิลด์ข้อมูลเมตา เช่น สร้างโดย แก้ไขโดย สร้างเมื่อ แก้ไขเมื่อ ฯลฯ
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Function Copy-AllFilesWithMetadata
{
param
(
[Parameter(Mandatory=$true)] [Microsoft.SharePoint.Client.Folder] $SourceFolder,
[Parameter(Mandatory=$true)] [Microsoft.SharePoint.Client.Folder] $TargetFolder
)
Try {
#Get all Files from the source folder
$SourceFilesColl = $SourceFolder.Files
$SourceFolder.Context.Load($SourceFilesColl)
$SourceFolder.Context.ExecuteQuery()
#Iterate through each file and copy
Foreach($SourceFile in $SourceFilesColl)
{
#Get the source file
$FileInfo = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($SourceFolder.Context, $SourceFile.ServerRelativeUrl)
#Copy File to the Target location
$TargetFileURL = $TargetFolder.ServerRelativeUrl+"/"+$SourceFile.Name
[Microsoft.SharePoint.Client.File]::SaveBinaryDirect($TargetFolder.Context, $TargetFileURL, $FileInfo.Stream,$True)
#Copy Metadata field values
$SourceListItem = $SourceFile.ListItemAllFields
$SourceFolder.Context.Load($SourceListItem)
$SourceFolder.Context.ExecuteQuery()
#Get the new file created
$TargetFile = $TargetFolder.Context.Web.GetFileByServerRelativeUrl($TargetFileURL)
$TargetListItem = $TargetFile.ListItemAllFields
#Set Metadata values from the source
$Author =$TargetFolder.Context.web.EnsureUser($SourceListItem["Author"].Email)
$TargetListItem["Author"] = $Author
$Editor =$TargetFolder.Context.web.EnsureUser($SourceListItem["Editor"].Email)
$TargetListItem["Editor"] = $Editor
$TargetListItem["Created"] = $SourceListItem["Created"]
$TargetListItem["Modified"] = $SourceListItem["Modified"]
$TargetListItem.Update()
$TargetFolder.Context.ExecuteQuery()
Write-host -f Green "Copied File '$($SourceFile.ServerRelativeUrl)' to '$TargetFileURL'"
}
#Process Sub Folders
$SubFolders = $SourceFolder.Folders
$SourceFolder.Context.Load($SubFolders)
$SourceFolder.Context.ExecuteQuery()
Foreach($SubFolder in $SubFolders)
{
If($SubFolder.Name -ne "Forms")
{
#Prepare Target Folder
$TargetFolderURL = $SubFolder.ServerRelativeUrl -replace $SourceLibrary.RootFolder.ServerRelativeUrl, $TargetLibrary.RootFolder.ServerRelativeUrl
Try {
$Folder=$TargetFolder.Context.web.GetFolderByServerRelativeUrl($TargetFolderURL)
$TargetFolder.Context.load($Folder)
$TargetFolder.Context.ExecuteQuery()
}
catch {
#Create Folder
if(!$Folder.Exists)
{
$TargetFolderURL
$Folder=$TargetFolder.Context.web.Folders.Add($TargetFolderURL)
$TargetFolder.Context.Load($Folder)
$TargetFolder.Context.ExecuteQuery()
Write-host "Folder Added:"$SubFolder.Name -f Yellow
}
}
#Call the function recursively
Copy-AllFilesWithMetadata -SourceFolder $SubFolder -TargetFolder $Folder
}
}
}
Catch {
write-host -f Red "Error Copying File!" $_.Exception.Message
}
}
#Set Parameter values
$SourceSiteURL="https://crescent.sharepoint.com/sites/sales"
$TargetSiteURL="https://crescent.sharepoint.com/sites/Ops"
$SourceLibraryName="Project Documents"
$TargetLibraryName="Documents"
#Setup Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the contexts
$SourceCtx = New-Object Microsoft.SharePoint.Client.ClientContext($SourceSiteURL)
$SourceCtx.Credentials = $Credentials
$TargetCtx = New-Object Microsoft.SharePoint.Client.ClientContext($TargetSiteURL)
$TargetCtx.Credentials = $Credentials
#Get the source library and Target Libraries
$SourceLibrary = $SourceCtx.Web.Lists.GetByTitle($SourceLibraryName)
$SourceCtx.Load($SourceLibrary)
$SourceCtx.Load($SourceLibrary.RootFolder)
$TargetLibrary = $TargetCtx.Web.Lists.GetByTitle($TargetLibraryName)
$TargetCtx.Load($TargetLibrary)
$TargetCtx.Load($TargetLibrary.RootFolder)
$TargetCtx.ExecuteQuery()
#Call the function
Copy-AllFilesWithMetadata -SourceFolder $SourceLibrary.RootFolder -TargetFolder $TargetLibrary.RootFolder
PnP PowerShell เพื่อคัดลอกไฟล์และโฟลเดอร์ระหว่างไซต์คอลเลกชัน
สคริปต์นี้จะคัดลอกไฟล์และโฟลเดอร์ระหว่างโฟลเดอร์ไลบรารีเอกสารจากไซต์คอลเลกชันหนึ่งไปยังอีกไซต์หนึ่งโดยใช้ PnP PowerShell cmdlet Copy-PnPFile ตรวจสอบให้แน่ใจว่าคุณแทนที่ $SiteURL ด้วย URL ของไซต์ต้นทาง $TargetFolderURL ด้วย URL ที่เกี่ยวข้องกับเซิร์ฟเวอร์ของไซต์ปลายทางของโฟลเดอร์ และ $SourceFolderURL ด้วย URL ที่เกี่ยวข้องกับเซิร์ฟเวอร์ของโฟลเดอร์ที่คุณต้องการคัดลอก
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$SourceFolderURL = "/sites/marketing/Branding/Logos"
$TargetFolderURL = "/sites/Purchase/Shared Documents"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Copy All Files and Folders between source and target folders
Copy-PnPFile -SourceUrl $SourceFolderURL -TargetUrl $TargetFolderURL -Force -OverwriteIfAlreadyExists
สคริปต์นี้จะคัดลอกโฟลเดอร์ “โลโก้” จากไลบรารี “การสร้างแบรนด์” ของไซต์การตลาดไปยังไลบรารี “เอกสารที่ใช้ร่วมกัน” ของไซต์การซื้อ คุณสามารถระบุโฟลเดอร์ที่มีอยู่ในไซต์ปลายทางได้เช่นกัน

