[玩转系统] 使用 PowerShell 将 SharePoint 2010 文档库迁移到 SharePoint Online
作者:精品下载站 日期:2024-12-14 15:33:25 浏览:14 分类:玩电脑
使用 PowerShell 将 SharePoint 2010 文档库迁移到 SharePoint Online
要求:将文档库从 SharePoint 2010 迁移到 SharePoint Online。
如何将 SharePoint 2010 文档库迁移到 SharePoint Online?
有多种方法可用于将文档库从 SharePoint 2010 迁移到 SharePoint Online,包括 SharePoint 迁移工具 (SPMT)、ShareGate 等第 3 方迁移工具等。这里是使用 PowerShell 的方法!其想法是将所有文档库从 SharePoint On-premises 导出到本地驱动器,将每个文档的元数据导出到 CSV 文件,然后将它们导入到 SharePoint Online。
在开始之前,请注意,这不是一个可以迁移元数据、安全性、版本历史记录等的成熟迁移脚本。该脚本仅复制文档库及其文件夹和元数据“创建者”、“创建于”、 SharePoint Online 的“修改者”和“修改时间”。如果您想迁移所有缺失的部分,您需要第 3 方迁移工具!
步骤 1:导出 SharePoint 本地文档库
第一步,登录到任何 SharePoint 本地 Web 前端服务器并运行以下 PowerShell 脚本,将单个文档库或所有文档库从 SharePoint 2010 导出到服务器的本地磁盘。根据您的环境更改脚本底部的运行时变量。请注意,此脚本导出给定网站(不是网站集)的库,您可以根据需要扩展它!
#region Runtime-Variables
$Global:SourceSiteURL=“https://SP2010.crescent.com/sites/marketing”
$Global:LocalFolderPath =“C:\Migration\Downloads”
#endregion 运行时变量
PowerShell 从 SharePoint 2010 导出所有文档库
此脚本将本地文档库从 SharePoint 2010 下载到本地磁盘。
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Function to Extract Metadata of a File to CSV File
Function Extract-SPFileMetadata([Microsoft.SharePoint.SPFile]$SPFile)
{
Try {
#Calculate URLs
$FileLibraryRelativeURL = $SPFile.ServerRelativeURL.Replace($SPFile.DocumentLibrary.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)
}
#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 $SPFile.DocumentLibrary.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.Email
$Metadata | Add-Member -MemberType NoteProperty -name "ModifiedBy" -value $SPFile.ModifiedBy.Email
$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
If (-not(Test-Path $global:MetadataFile))
{
$Metadata | Export-Csv $global:MetadataFile -NoTypeInformation
}
else
{
#Append data to Existing CSV File - Remove Duplicates
$CSVData = [Object[]] (Import-Csv $global:MetadataFile)
$CSVData + $Metadata | Sort FileAbsoluteURL -unique | Export-Csv $global:MetadataFile -NoTypeInformation
}
Write-host -f Green "`tMetadata Extracted from the File:"$SPFile.ServerRelativeURL
}
Catch {
write-host -f Red "Error Getting Metadatas:" $_.Exception.Message
}
}
#Function to Download All Files from a SharePoint Folder
Function Download-SPFolder($SPFolderURL, $LocalFolderPath)
{
Try {
#Get the Source SharePoint Folder
$SPFolder = $global:web.GetFolder($SPFolderURL)
$LocalFolderPath = Join-Path $LocalFolderPath $SPFolder.Name
#Ensure the destination local folder exists!
If (!(Test-Path -path $LocalFolderPath))
{
#If it doesn't exist, Create
$LocalFolder = New-Item $LocalFolderPath -type directory
}
#Loop through each file in the folder and download it to Destination
ForEach ($File in $SPFolder.Files)
{
#Download the file
$Data = $File.OpenBinary()
$FilePath= Join-Path $LocalFolderPath $File.Name
[System.IO.File]::WriteAllBytes($FilePath, $data)
Write-host -f Green "`tDownloaded the File:"$File.ServerRelativeURL
#Get the Metadata of the File
Extract-SPFileMetadata -SPFile $File
}
#Process the Sub-Folders & Recursively call the function
ForEach ($SubFolder in $SPFolder.SubFolders)
{
If($SubFolder.Name -ne "Forms") #Leave "Forms" Folder
{
#Call the function Recursively
Download-SPFolder $SubFolder $LocalFolderPath
}
}
}
Catch {
Write-host -f Red "Error Downloading Document Library:" $_.Exception.Message
}
}
#Function to export a Single Library
Function Export-SPLibrary()
{
param
(
[Parameter(Mandatory=$true)] [string] $LibraryName
)
Try {
#Get the Source Web
$global:Web = Get-SPWeb $global:SourceSiteURL
$global:MetadataFile = "$global:LocalFolderPath\Metadata.csv"
#Get the library
$Library = $Global:Web.Lists.TryGetList($LibraryName)
#Get All Files from the Library and export
If($Library -ne $Null)
{
Write-host -f magenta "Downloading Document Library:" $Library.Title
#Call the function to download the document library
Download-SPFolder -SPFolderURL $Library.RootFolder.Url -LocalFolderPath $global:LocalFolderPath
}
Else
{
Write-host -f Yellow "Could not Find Document Library:" $LibraryName
}
Write-host "*** Completed downloading Library '$LibraryName' ***"
}
Catch {
Write-host -f Red "Error Downloading Document Library:" $_.Exception.Message
}
}
#Function to export all libraries in a SharePoint Site
Function Export-SPAllLibraries()
{
Try {
#Get the Source Web
$global:Web = Get-SPWeb $global:SourceSiteURL
#Delete any existing files and folders in the download location
If (Test-Path $global:LocalFolderPath) {Get-ChildItem -Path $global:LocalFolderPath -Recurse| ForEach-object {Remove-item -Recurse -path $_.FullName }}
#Arry to Skip System Libraries
$SystemLibraries =@("Pages", "Converted Forms", "Master Page Gallery", "Customized Reports",
"Form Templates", "Images", "List Template Gallery", "Theme Gallery", "Reporting Templates",
"Site Collection Documents", "Site Collection Images", "Site Pages", "Solution Gallery",
"Style Library", "Web Part Gallery","Site Assets", "wfpub")
#Get all document libraries - Exclude Hidden and System Libraries
$LibraryCollection = $Web.lists | Where-Object { ($_.BaseType -eq "DocumentLibrary") -and ($_.hidden -eq $false) -and ($SystemLibraries -notcontains $_.Title)}
#Iterate through each list and export
ForEach($Library in $LibraryCollection)
{
Write-host -f magenta "Downloading Document Library:" $Library.Title
#Call the function to download the document library
Download-SPFolder -SPFolderURL $Library.RootFolder.Url -LocalFolderPath $global:LocalFolderPath
}
Write-host "*** Downloaded All Libraries From the Given Site! ***"
}
Catch {
Write-host -f Red "Error Downloading All Document Libraries:" $_.Exception.Message
}
}
#region Runtime-Variables
$Global:SourceSiteURL = "https://SP2010.crescent.com/sites/marketing"
$Global:LocalFolderPath ="C:\Migration\Downloads"
#endregion Runtime-Variables
#Call the Function to export all document libraries from a site
Export-SPLibraries
#To Export a specific library, use:
#Export-SPLibrary -LibraryName "Shared Documents"
运行脚本。执行后,此脚本会将所有文档库下载到给定的 LocalFolderPath。使用此脚本,您可以从 SharePoint 本地网站下载所有文档库或特定文档库。
此外,该脚本还将文档的元数据提取到给定 $LocalFolderPath 中名为“Metadata.csv”的 CSV 文件中。
如果需要更新元数据
metadata.csv 文件包含“创建者”、“修改者”等字段的文件元数据。确保 CSV 文件中列出的电子邮件 ID 在 SharePoint Online 环境中有效。如果需要,您可以更新此 CSV 文件并保存。
步骤 2. 将文档库导入 SharePoint Online
现在,将下载的文件夹(此处为“C:\Migration\Downloads”)复制到您的笔记本电脑或安装了 SharePoint Online 客户端 SDK 的任何计算机,并且可以访问目标 SharePoint Online 网站。将以下脚本复制到 PowerShell ISE 并根据您的环境更改突出显示的运行时变量。确保 TargetSiteURL 中给出的站点已创建,然后执行脚本!
将 SharePoint 2010 文档库上传到 SharePoint Online 并设置元数据
让我们使用 PowerShell cmdlet 将本地内容上传到 SharePoint Online:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#Function to Ensure SharePoint Online User
Function Ensure-SPOUser([string]$UserID)
{
Try {
$User = $Global:Web.EnsureUser($UserID)
$Global:Ctx.Load($User)
$Global:Ctx.ExecuteQuery()
Return $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 SetSPO-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
$Global:Ctx.Load($TargetLibrary.RootFolder)
$Global:Ctx.ExecuteQuery()
$FileLibraryRelativeURL = $File.ServerRelativeUrl.Replace($TargetLibrary.RootFolder.ServerRelativeUrl,[string]::Empty)
#Import Metadata CSV File
$MetadataCSVPath = "$Global:SourcePath\Metadata.csv"
$MetadataFile = Import-Csv -LiteralPath $MetadataCSVPath
#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
$Author = Ensure-SPOUser -UserID $Metadata.CreatedBy
$Editor= Ensure-SPOUser -UserID $Metadata.ModifiedBy
#Set Metadata of the File
$ListItem = $File.ListItemAllFields
If($Author -ne $Null)
{
$Listitem["Author"] = $Author
}
If($Editor -ne $Null)
{
$ListItem["Editor"] = $Editor
}
$ListItem["Created"] = $Metadata.CreatedOn
$ListItem["Modified"] = $Metadata.ModifiedOn
$ListItem.Update()
$Global:Ctx.ExecuteQuery()
Write-host -f Green "`t`t`tMetadata for '$($File.ServerRelativeURL)' has been Updated Successfully!"
}
}
Catch {
write-host -f Red "`t`t`tError updating Metadata of the Document:" $_.Exception.Message
}
}
#Function to migrate all Files and Folders from Local Path to SharePoint Online
Function MigrateSPO-LocalFolder()
{
param
(
[Parameter(Mandatory=$true)] [string] $SourceFolderPath,
[Parameter(Mandatory=$true)] [Microsoft.SharePoint.Client.List] $TargetLibrary
)
Try {
#Get the Target Folder to Upload
$TargetFolder = $TargetLibrary.RootFolder
$Global:Ctx.Load($TargetFolder)
$Global:Ctx.ExecuteQuery()
#Get All Files and Folders from the Source
Get-ChildItem $SourceFolderPath -Recurse | ForEach-Object {
If ($_.PSIsContainer -eq $True) #If its a Folder!
{
$TargetFolderRelativeURL = $TargetFolder.ServerRelativeURL+$_.FullName.Replace($SourceFolderPath,[string]::Empty).Replace("\","/")
Write-host -f Yellow "`t`tEnsuring Folder '$TargetFolderRelativeURL'"
#Check If Folder Exists
Try {
$Folder = $Global:Web.GetFolderByServerRelativeUrl($TargetFolderRelativeURL)
$Global:Ctx.Load($Folder)
$Global:Ctx.ExecuteQuery()
Write-host -f Magenta "`t`t`tFolder Already Exists: $TargetFolderRelativeURL"
}
Catch {
#Create New Sub-Folder
$Folder= $Global:Web.Folders.Add($TargetFolderRelativeURL)
$Global:Ctx.ExecuteQuery()
Write-host -f Green "`t`t`tCreated Folder at "$TargetFolderRelativeURL
$Global:Ctx.ExecuteQuery()
}
}
Else #Its a File, Upload it!
{
#Calculate Source and Destination File Paths
$TargetFileURL = $TargetFolder.ServerRelativeURL + $_.DirectoryName.Replace($SourceFolderPath,[string]::Empty).Replace("\","/") +"/"+$_.Name
$SourceFilePath = $_.FullName
Write-host -f Yellow "`t`tUploading File '$_' to URL "$TargetFileURL
#Get the file from disk
$FileStream = ([System.IO.FileInfo] (Get-Item $SourceFilePath)).OpenRead()
#Upload the File to SharePoint Library's Folder
$FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$FileCreationInfo.Overwrite = $True
$FileCreationInfo.ContentStream = $FileStream
$FileCreationInfo.URL = $TargetFileURL
$FileUploaded = $TargetFolder.Files.Add($FileCreationInfo)
$Global:Ctx.ExecuteQuery()
$FileStream.Close()
Write-host "`t`t`tFile '$TargetFileURL' Uploaded Successfully!" -ForegroundColor Green
#get the uploaded file
$File = $Global:Web.GetFileByServerRelativeUrl($TargetFileURL)
$Global:Ctx.Load($File)
$Global:Ctx.ExecuteQuery()
#Update Metadata of the File
SetSPO-DocumentMetadata -File $File -TargetLibrary $TargetLibrary
}
}
}
Catch {
write-host -f Red "`t`t`tError Uploading File:" $_.Exception.Message
}
}
#Function to Ensure a SharePoint Online document library
Function EnsureSPO-DocumentLibrary()
{
param
(
[Parameter(Mandatory=$true)] [string] $DocumentLibraryName
)
Try {
Write-host -f Yellow "`nEnsuring Document Library '$DocumentLibraryName'"
#Get All Existing Lists from the web
$Lists = $Global:Web.Lists
$Global:Ctx.Load($Lists)
$Global:Ctx.ExecuteQuery()
#Check if the Library name doesn't exist already
If(!($Lists.Title -contains $DocumentLibraryName))
{
#Create Document Library
$ListInfo = New-Object Microsoft.SharePoint.Client.ListCreationInformation
$ListInfo.Title = $DocumentLibraryName
$ListInfo.TemplateType = 101 #Document Library
$List = $Global:Web.Lists.Add($ListInfo)
$List.Update()
$Global:Ctx.ExecuteQuery()
write-host -f Green "`tNew Document Library '$DocumentLibraryName' has been created!"
Return $List
}
Else
{
#Get the Library
$List = $Global:Web.Lists.GetByTitle($DocumentLibraryName)
$Global:Ctx.Load($List)
$Global:Ctx.ExecuteQuery()
Return $List
Write-Host -f Magenta "`tA Document Library '$DocumentLibraryName' Already exist!"
}
}
Catch {
write-host -f Red "`tError Creating Document Library!" $_.Exception.Message
}
}
#Main Function
Function Import-SPOLibraries()
{
Try {
#Setup the context
$Global:Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($Global:TargetSiteURL)
$Global:Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Global:Cred.UserName,$Global:Cred.Password)
#Get the Web
$Global:Web = $Global:Ctx.Web
$Global:Ctx.Load($Global:Web)
$Global:Ctx.ExecuteQuery()
#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 = EnsureSPO-DocumentLibrary -DocumentLibraryName $SourceLibrary.Name
#Migrate Files and Folders from the Source to the Destination
MigrateSPO-LocalFolder -SourceFolderPath $SourceLibrary.FullName -TargetLibrary $TargetLibrary
}
Write-host -f Cyan "`n*** Import Completed! ***`n"
}
Catch {
write-host -f Red "Error:" $_.Exception.Message
}
}
#region Runtime-Variables
$Global:SourcePath = "C:\Migration\Downloads"
$Global:TargetSiteURL = "https://crescent.sharepoint.com/Sites/Marketing"
#endregion Runtime-Variables
#Get Credentials to connect
$Global:Cred = Get-Credential -Message "Enter the Admin Credentials:"
#Call the function to import document libraries and files to SharePoint Online
Import-SPOLibraries
该脚本是为了逐个站点复制文档库而编写的,并且可以进一步扩展!
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[短剧合集] 2025年06月2日 精选+付费短剧推荐39部
[软件合集] 25年6月2日 精选软件18个
[软件合集] 25年6月1日 精选软件15个
[短剧合集] 2025年06月1日 精选+付费短剧推荐59部
[短剧] 2025年05月31日 精选+付费短剧推荐58部
[软件合集] 25年5月31日 精选软件66个
[电影] 黄沙漫天(2025) 4K.EDRMAX.杜比全景声 / 4K杜比视界/杜比全景声
[风口福利] 短视频红利新风口!炬焰创作者平台重磅激励来袭
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[美图] 2W美女个美女小姐姐,饱眼福
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag