ค้นหาเว็บไซต์

คัดลอกไลบรารีเอกสารระหว่างผู้เช่าใน SharePoint Online โดยใช้ PowerShell


ความต้องการ: คัดลอกไลบรารีเอกสารที่มีไฟล์และโฟลเดอร์จากผู้เช่า SharePoint Online รายหนึ่งไปยังอีกรายหนึ่ง

วิธีคัดลอกไลบรารีเอกสาร SharePoint Online ไปยังผู้เช่าได้อย่างไร

มีเครื่องมือของบุคคลที่สามที่ต้องชำระเงินมากมาย เช่น ShareGate, Metalogix ฯลฯ ที่จะทำเช่นนี้ได้อย่างมีประสิทธิภาพ และเราสามารถใช้ PowerShell (โดยมีข้อจำกัด) เพื่อดำเนินการย้ายข้อมูลนี้ได้ ไลบรารีเอกสารส่งออกนี้

โปรดทราบว่ามีข้อจำกัดในวิธี PowerShell นี้ เช่นเดียวกับที่สคริปต์นี้ไม่ได้คัดลอก:

  • ประวัติเวอร์ชัน
  • สิทธิ์
  • ขั้นตอนการทำงาน
  • การตั้งค่าไลบรารีเอกสาร
  • ข้อมูลเมตาที่จำกัด ฯลฯ

จากพารามิเตอร์ที่กำหนด สคริปต์ส่งออกจะดาวน์โหลดไลบรารีเอกสารเดียวพร้อมไฟล์และโฟลเดอร์หรือไลบรารีทั้งหมดจากไซต์ต้นทางพร้อมกับค่าฟิลด์ข้อมูลเมตา “สร้าง”, “สร้างโดย”, “แก้ไข” และ “แก้ไขโดย” ไปยังดิสก์ภายในเครื่อง จากนั้น คุณสามารถใช้สคริปต์นำเข้าเพื่ออัปโหลดไลบรารีเอกสารไปยังไซต์เป้าหมายได้

ส่งออกไลบรารีเอกสารใน SharePoint Online โดยใช้ PowerShell

ตั้งค่าพารามิเตอร์ในส่วน พารามิเตอร์ #region ตามสภาพแวดล้อมของคุณและเรียกใช้สคริปต์ วิธีนี้จะคัดลอกไฟล์และโฟลเดอร์ทั้งหมดและแยกข้อมูลเมตาไปยังดิสก์ในเครื่องของคุณ


#Function to Extract Metadata of a File to CSV File
Function Extract-PnPFileMetadata([Microsoft.SharePoint.Client.File]$SPFile)
{
    Try {
        #Calculate URLs
        $FileLibraryRelativeURL = $SPFile.ServerRelativeURL.Replace($global:Library.RootFolder.ServerRelativeURL,[string]::Empty)
        #Calculate Absolute URL
        If($global:Web.ServerRelativeUrl -eq "/")
        {
            $FileAbsoluteURL=  $("{0}{1}" -f $global:Web.Url, $SPFile.ServerRelativeUrl)
        }
        Else
        {
            $FileAbsoluteURL=  $("{0}{1}" -f $global:Web.Url.Replace($global:Web.ServerRelativeUrl,[string]::Empty), $SPFile.ServerRelativeUrl)
        }
        #Get Autor, Editor of the file 
        Get-PnPProperty -ClientObject $SPFile -Property Author, ModifiedBy

        #Extract the Metadata of the file
        $Metadata = New-Object PSObject 
        $Metadata | Add-Member -MemberType NoteProperty -name "FileName" -value $SPFile.Name
        $Metadata | Add-Member -MemberType NoteProperty -name "ParentLibrary" -value $global:Library.Title
        $Metadata | Add-Member -MemberType NoteProperty -name "FileAbsoluteURL" -value $FileAbsoluteURL
        $Metadata | Add-Member -MemberType NoteProperty -name "FileLibraryRelativeURL" -value $FileLibraryRelativeURL
        $Metadata | Add-Member -MemberType NoteProperty -Name "CreatedBy" -value $SPFile.Author.LoginName
        $Metadata | Add-Member -MemberType NoteProperty -name "ModifiedBy" -value $SPFile.ModifiedBy.LoginName
        $Metadata | Add-Member -MemberType NoteProperty -name "CreatedOn" -value $SPFile.TimeCreated 
        $Metadata | Add-Member -MemberType NoteProperty -name "ModifiedOn" -value $SPFile.TimeLastModified
 
        #Send the Metadata to CSV File
        $Metadata | Export-Csv $global:MetadataFile -NoTypeInformation -Append
        Write-host -f Green "`t`tMetadata Extracted from the File:"$SPFile.ServerRelativeURL
    }
    Catch {
        write-host -f Red "Error Getting Metadata:" $_.Exception.Message
    }
}

#Function to Download a Document Library from SharePoint Online site
Function Export-PnPLibrary()
{
    param
    (
        [Parameter(Mandatory=$true)] [string]$LibraryName
    )

    Try {
        Write-host "Exporting Library:"$LibraryName -f Yellow

        #Get the Library
        $global:Library = Get-PnPList -Identity $LibraryName -Includes RootFolder

        #Create a Local Folder for Document Library, if it doesn't exist
        $LocalFolder = $global:DownloadPath +"\" +$global:Library.RootFolder.Name
        If (!(Test-Path -Path $LocalFolder)) {
                New-Item -ItemType Directory -Path $LocalFolder | Out-Null
        }
        Write-host -f Yellow "`tEnsured Folder for Document Library '$LocalFolder'"

        #Get all Items from the Library - with progress bar
        $global:counter = 0
        $LibraryItems = Get-PnPListItem -List $LibraryName -PageSize 500 -Fields ID,FieldValues -ScriptBlock { Param($items) $global:counter += $items.Count; Write-Progress -PercentComplete `
                    ($global:Counter / ($global:Library.ItemCount) * 100) -Activity "Getting Items from Library:" -Status "Processing Items $global:Counter to $($global:Library.ItemCount)";} 
        Write-Progress -Activity "Completed Retrieving Folders from Library $LibraryName" -Completed

        #Get all Subfolders of the library
        $SubFolders = $LibraryItems | Where {$_.FileSystemObjectType -eq "Folder" -and $_.FieldValues.FileLeafRef -ne "Forms"}
        $SubFolders | ForEach-Object {
            #Ensure All Folders in the Local Path
            $LocalFolder = $global:DownloadPath + ($_.FieldValues.FileRef.Substring($global:Web.ServerRelativeUrl.Length)) -replace "/","\"
            #Create Local Folder, if it doesn't exist
            If (!(Test-Path -Path $LocalFolder)) {
                    New-Item -ItemType Directory -Path $LocalFolder | Out-Null
            }
            Write-host -f Yellow "`tEnsured Folder '$LocalFolder'"
        }

        #Get all Files from the folder
        $FilesColl =  $LibraryItems | Where {$_.FileSystemObjectType -eq "File"}

        $global:Filess = $FilesColl
        #Iterate through each file and download
        $FilesColl | ForEach-Object {
            Try {
                $FileDownloadPath = ($global:DownloadPath + ($_.FieldValues.FileRef.Substring($global:Web.ServerRelativeUrl.Length)) -replace "/","\").Replace($_.FieldValues.FileLeafRef,[string]::Empty)
                Get-PnPFile -Url $_.FieldValues.FileRef -Path $FileDownloadPath -FileName $_.FieldValues.FileLeafRef -AsFile -Force -ErrorAction Stop
                Write-host -f Green "`tDownloaded File from '$($_.FieldValues.FileRef)'"
                
                #Get the Metadata of the File
                $File = Get-PnPProperty -ClientObject $_ -Property File
                Extract-PnPFileMetadata -SPFile $File
               }
            Catch {
                write-host -f Red "`tError Downloading File from '$($_.FieldValues.FileRef)' : "$_.Exception.Message
            }
        }
    }
    Catch {
        write-host -f Red "`tError:" $_.Exception.Message
    }
}

#Function to export all libraries in a SharePoint Site
Function Export-PnPLibraries()
{
    Try { 
        #Arry to Skip System Lists and Libraries
        $SystemLists =@("Converted Forms", "Master Page Gallery", "Customized Reports", "Form Templates", "List Template Gallery", "Theme Gallery",
               "Reporting Templates", "Solution Gallery", "Style Library", "Web Part Gallery","Site Assets", "wfpub", "Site Pages", "Images")
     
        #Filter Document Libraries to Scan 
        $LibraryCollection = Get-PnPList | Where {$_.BaseType -eq "DocumentLibrary" -and $_.Hidden -eq $false -and $SystemLists -notcontains $_.Title -and $_.ItemCount -gt 0}
        
        #Loop through each document library
        ForEach($Library in $LibraryCollection)
        {
            #Call the function to download the document library
            Export-PnPLibrary -LibraryName $Library.Title
        }
    }
    Catch {
        Write-host -f Red "Error Downloading Libraries:" $_.Exception.Message
    }
}

#region Parameters
$SourceSiteURL = "https://crescent.sharepoint.com/sites/ops"
$LibraryName = "Branding"
$global:DownloadPath= "C:\Temp\Migration"
#endregion Parameters

#Connect to SharePoint Online
Connect-PnPOnline $SourceSiteURL -Interactive
$global:Web = Get-PnPWeb
$global:MetadataFile = "$global:DownloadPath\Metadata.csv"

#Delete any existing files and folders in the download location
If (Test-Path $global:DownloadPath) {Get-ChildItem -Path $global:DownloadPath -Recurse| ForEach-object {Remove-item -Recurse -path $_.FullName }}

#Call the function to download a library
Export-PnPLibrary -LibraryName $LibraryName

#To Export all libraries, use:
#Export-PnPLibraries

นำเข้าไลบรารีเอกสารไปยัง SharePoint Online

เมื่อส่งออกแล้ว คุณสามารถนำเข้าไลบรารีเอกสารไปยังผู้เช่ารายอื่นได้ด้วยสคริปต์ PowerShell ด้านล่าง เพียงตรวจสอบให้แน่ใจว่า ID ผู้ใช้นั้นถูกต้องในไฟล์ "Metadata.csv" จากสคริปต์ด้านบน หากจำเป็น คุณสามารถแทนที่รหัสให้ตรงกับผู้เช่าปลายทางได้


#Function to Ensure SharePoint Online User
Function Ensure-PnPUser([string]$UserID)
{
    Try {
        #Try to Get the User
        $User = Get-PnPUser -Identity $UserID

        If($User -eq $null) {
            $User = New-PnPUser -LoginName $UserID
        }
        #Return the User Object
        $User
    }
    Catch {
        write-host -f Red "`t`t`tError Resolving User $UserID :" $_.Exception.Message
        Return $Null
    }
}

#Function to Set the Metadata of a Document
Function SetPnP-DocumentMetadata()
{
    param
    (
        [Parameter(Mandatory=$true)] [Microsoft.SharePoint.Client.File] $File,
        [Parameter(Mandatory=$true)] [Microsoft.SharePoint.Client.List] $TargetLibrary
    )    
    Try {
        #Calculate the Library Relative URL of the File
        $TargetFolder = Get-PnPProperty -ClientObject $TargetLibrary -Property RootFolder
        $FileLibraryRelativeURL = $File.ServerRelativeUrl.Replace($TargetLibrary.RootFolder.ServerRelativeUrl,[string]::Empty)        
        $FileItem = Get-PnPProperty -ClientObject $File -Property ListItemAllFields

        #Import Metadata CSV File
        $MetadataFile = Import-Csv -LiteralPath $global:MetadataFile
        #Get the Metadata of the File
        $Metadata = $MetadataFile | Where-Object {($_.ParentLibrary -eq ($TargetLibrary.Title)) -and $_.FileLibraryRelativeURL -eq $FileLibraryRelativeURL}
        If($Metadata)
        {
            Write-host -f Yellow "`t`tUpdating Metadata for File '$($File.ServerRelativeURL)'" 

            #Get 'Created By' and 'Modified By' Users
            $FileMetadata = @{}
            $Author = Ensure-PnPUser -UserID $Metadata.CreatedBy
            $Editor = Ensure-PnPUser -UserID $Metadata.ModifiedBy
            $FileMetadata.add("Created",[DateTime]$Metadata.CreatedOn)
            $FileMetadata.add("Modified",[DateTime]$Metadata.ModifiedOn)

            If($Author -ne $Null)
            {
                $FileMetadata.add("Author", $Author.LoginName)
            }
            If($Editor -ne $Null)
            {
                $FileMetadata.add("Editor", $Editor.LoginName)
            }
            #Update document properties
            Set-PnPListItem -List $TargetLibrary -Identity $FileItem.Id -Values $FileMetadata | Out-Null 
            Write-host -f Green "`t`t`tMetadata has been Updated Successfully!"
        }
    }
    Catch {
        write-host -f Red "`t`t`tError updating Metadata of the Document:"$_.Exception.Message
    }
}
 
#Function to Import all Files and Folders from Local Folder to SharePoint Online
Function ImportPnP-Library()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SourceLibraryPath,
        [Parameter(Mandatory=$true)] [Microsoft.SharePoint.Client.List] $TargetLibrary
    )
    Try {
        #Get the Target Folder to Upload
        $TargetFolder = Get-PnPProperty -ClientObject $TargetLibrary -Property RootFolder
        $TargetFolderSiteRelativeURL = $TargetFolder.ServerRelativeURL.Replace($global:Web.ServerRelativeUrl+"/",[string]::Empty)

        Get-ChildItem $SourceLibraryPath -Recurse | ForEach-Object {
            $TargetFolderRelativeURL = $TargetFolderSiteRelativeURL+$_.FullName.Replace($SourceLibraryPath,[string]::Empty).Replace("\","/")
            #write-host $TargetFolderRelativeURL
            If ($_.PSIsContainer -eq $True) #If its a Folder, ensure it!
            {
                Write-host -f Yellow "`t`tEnsuring Folder '$TargetFolderRelativeURL'"
                #Ensure Folder
                Resolve-PnPFolder -SiteRelativePath $TargetFolderRelativeURL | Out-Null
            }
            Else #Its a File, Upload it!
            {
                #Calculate the Parent Folder for File
                $TargetFolderURL = (Split-Path $TargetFolderRelativeURL -Parent).Replace("\","/")                
                $SourceFilePath = $_.FullName
  
                Write-host -f Yellow "`t`tUploading File '$_' to Folder:"$TargetFolderURL
                $File = Add-PnPFile -Path $SourceFilePath -Folder $TargetFolderURL 
                Write-host "`t`t`tFile Uploaded Successfully!" -ForegroundColor Green 
                 
                #Update Metadata of the File
                SetPnP-DocumentMetadata -File $File -TargetLibrary $TargetLibrary
            }
        }
    }
    Catch {
        write-host -f Red "`t`t`tError Importing Library:" $_.Exception.Message
    }
}
 
#Function to Ensure a SharePoint Online document library
Function EnsurePnP-DocumentLibrary()
{
    param
    (
        [Parameter(Mandatory=$true)] [string] $LibraryName
    )    
    Try {
        Write-host -f Yellow "`nEnsuring Library '$LibraryName'"
         
        #Check if the Library exist already
        $List = Get-PnPList | Where {$_.Title -eq $LibraryName} 

        If($List -eq $Null)
        {
            #Create Document Library
            $List = New-PnPList -Title $LibraryName -Template DocumentLibrary -OnQuickLaunch  
            write-host  -f Green "`tNew Document Library '$LibraryName' has been created!"
        }
        Else
        {
            #Get the Library
            $List = Get-PnPList -Identity $LibraryName
            Write-Host -f Magenta "`tA Document Library '$LibraryName' Already exist!"
        }
        Return $List
    }
    Catch {
        write-host -f Red "`tError Creating Document Library!" $_.Exception.Message
    }
}
 
#Main Function
Function Import-PnPLibraries()
{
    Try {
        #Get Top Level Folders from the Source as "Document Libraries"
        $SourceLibraries = Get-ChildItem -Directory -Path $Global:SourcePath
 
        #Create Document Libraries
        ForEach($SourceLibrary in $SourceLibraries)
        {
            #call the function to Ensure document library
            $TargetLibrary = EnsurePnP-DocumentLibrary -LibraryName $SourceLibrary.Name
 
            #Import Files and Folders from the Source to the Destination
            ImportPnP-Library -SourceLibraryPath $SourceLibrary.FullName -TargetLibrary $TargetLibrary
        }
    }
    Catch {
        write-host -f Red "Error:" $_.Exception.Message
    }
}
 
#region Parameters
$Global:SourcePath = "C:\Temp\Migration"
$Global:TargetSiteURL = "https://national.sharepoint.com/Sites/Operations"
#endregion Parameters
 
#Connect to SharePoint Online
Connect-PnPOnline $TargetSiteURL -Interactive
$global:Web = Get-PnPWeb
$global:MetadataFile = "$global:SourcePath\Metadata.csv"

#Import a Single Document Library
$Library = EnsurePnP-DocumentLibrary "Mason"
ImportPnP-Library -SourceLibraryPath "$Global:SourcePath\Mason" -TargetLibrary $Library

#Call the function to import  all document libraries from the source path
#Import-PnPLibraries

คุณสามารถใช้สคริปต์ PowerShell นี้เพื่อสำรองข้อมูลไลบรารีเอกสารใน SharePoint Online ได้เช่นกัน

กระทู้ที่เกี่ยวข้อง:

  • เมื่อต้องการคัดลอกไลบรารีเอกสารใน SharePoint Online: วิธีคัดลอกไลบรารีเอกสารใน SharePoint Online
  • เมื่อต้องการคัดลอกไลบรารีเอกสารจากไซต์หนึ่งไปยังอีกไซต์หนึ่งใน SharePoint Online โดยใช้ PowerShell ให้ใช้: SharePoint Online: วิธีคัดลอกไลบรารีเอกสารไปยังไซต์อื่น

บทความที่เกี่ยวข้อง