使用 PowerShell 迁移到 SharePoint

本文介绍基于 SharePoint 迁移工具 (SPMT) 迁移引擎的新 PowerShell cmdlet。 它们可用于将文件从 SharePoint 2010 和 SharePoint 2013 本地文档库和列表项以及文件共享移动到 Microsoft 365。 有关与 SharePoint 迁移相关的所有当前 PowerShell cmdlet 的信息,请参阅 Microsoft SharePoint 迁移工具 cmdlet 参考

PowerShell cmdlet 的功能与 SharePoint 迁移工具相同。

注意

由中国世纪互联运营的 Office 365 的用户暂无法使用这些 PowerShell cmdlet。

系统要求

为实现最佳性能而建议满足的要求

说明 建议
CPU 64 位四核处理器或更高
RAM 16 GB
本地存储 硬盘:150GB 可用空间
操作系统 Windows Server 2016 Standard 或 Datacenter
Windows Server 2012 R2
Windows 10 客户端
.NET Framework 4.6.2
Microsoft Visual C++ 2015 Redistributable OneNote 迁移所必需

重要

需要 PowerShell 5.0x 和 .NET Framework 4.6.2 或更高版本来支持最多 400 个字符的文件路径的迁移。

最低要求(预期性能较差)

说明 最低要求
CPU 64 位 1.4 GHz 双核处理器或更高
RAM 8 GB
本地存储 硬盘:150 GB 可用空间
网卡 高速 Internet 连接
操作系统 Windows Server 2008 R2
Windows 7
Windows 8 或 8.1
.NET Framework 4.6.2
Microsoft Visual C++ 2015 Redistributable OneNote 迁移所必需。
PowerShell 需要 PowerShell 5.x 来支持最多 400 个字符的文件路径的迁移。 不支持 PowerShell 6.0 或更高版本。

开始之前

  1. 使用现有的 Active Directory 或用于将帐户添加到 Microsoft 365 的其他选项之一预配 Microsoft 365。 有关详细信息,请参阅 Microsoft 365 与本地环境的集成将用户添加到Microsoft 365 商业应用版

  2. 打开文件夹:

    $env:UserProfile\Documents\WindowsPowerShell\Modules\Microsoft.SharePoint.MigrationTool.PowerShell

    请确保里面有DLLs,如果你使用OneDrive,你可能需要将WindowsPowershell文件夹复制到OneDrive / 文档中。

  3. 从此位置运行以下 PowerShell 命令

    Import-Module Microsoft.SharePoint.MigrationTool.PowerShell
    

创建和初始化迁移会话

  • Register-SPMTMigration
    此 cmdlet 创建并初始化迁移会话。 初始化在会话级别配置迁移设置。 如果未定义特定的设置参数,将使用默认设置。 注册会话后,可以将任务添加到会话并开始迁移。

添加迁移任务

  • Add-SPMTTask
    使用此 cmdlet 将新的迁移任务添加到已注册的迁移会话。 目前允许三种不同类型的任务:文件共享任务、SharePoint 任务和 JSON 定义的任务。 注意:不允许重复任务。

删除任务

  • Remove-SPMTTask
    此 cmdlet 可用于从已注册的迁移会话中删除现有迁移任务。

开始迁移

返回当前会话对象

  • Get-SPMTMigration
    返回当前会话的 对象。 这包括当前任务的状态和当前会话级别设置。 当前任务状态包括:
    • 已扫描文件数
    • 已迁移文件数
    • 任何迁移错误消息

停止当前迁移

在控制台中显示迁移状态详细信息

  • Show-SPMTMigration
    如果在 NoShow 模式下启动迁移,运行 Show-SPMTMigration cmdlet 会在控制台中显示任务 ID、数据源位置、目标位置和迁移状态。 按 Ctrl+C 将返回到 NoShow 模式。

删除迁移会话

示例应用场景

示例 1:IT 管理员添加 SharePoint 本地任务,并在后台开始迁移。

#Define SharePoint 2013 data source#
$SourceSiteUrl = "http://YourOnPremSite/"
$OnPremUserName = "Yourcomputer\administrator"
$OnPremPassword = ConvertTo-SecureString -String "OnPremPassword" -AsPlainText -Force
$SPCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $OnPremUserName, $OnPremPassword
$SourceListName = "SourceListName"

#Define SharePoint target#
$SPOUrl = "https://contoso.sharepoint.com"
$UserName = "admin@contoso.onmicrosoft.com"
$PassWord = ConvertTo-SecureString -String "YourSPOPassword" -AsPlainText -Force
$SPOCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $UserName, $PassWord
$TargetListName = "TargetListName"

#Define File Share data source#
$FileshareSource = "YourFileShareDataSource"

#Import SPMT Migration Module#
Import-Module Microsoft.SharePoint.MigrationTool.PowerShell

#Register the SPMT session with SharePoint credentials#
Register-SPMTMigration -SPOCredential $SPOCredential -Force

#Add two tasks into the session. One is SharePoint migration task, and another is File Share migration task.#
Add-SPMTTask -SharePointSourceCredential $SPCredential -SharePointSourceSiteUrl $SourceSiteUrl  -TargetSiteUrl $SPOUrl -MigrateAll
Add-SPMTTask -FileShareSource $FileshareSource -TargetSiteUrl $SPOUrl -TargetList $TargetListName

#Start Migration in the console. #
Start-SPMTMigration

示例 2:IT 管理员希望将迁移从后台“NoShow 模式”移到前台,并在 cmdlet 下运行,以便在控制台中显示迁移进度。

Show-SPMTMigration

示例 3:IT 管理员希望通过加载 .csv 文件来执行批量迁移。 此示例中的示例文件是 SPMT.csv。

Load CSV;
$csvItems = import-csv "C:\spmt.csv" -Header c1,c2,c3,c4,c5,c6
ForEach ($item in $csvItems)
{
    Write-Host $item.c1
    Add-SPMTTask -FileShareSource $item.c1 -TargetSiteUrl $item.c4 -TargetList $item.c5 -TargetListRelativePath $item.c6
}

spmt.csv文件中定义了两个迁移任务。

D:\MigrationTest\Files\Average_1M\c,,,https://SPOSite.sharepoint.com,Documents,Test
C:\work\Powershell\negative,,,https://SPOSite.sharepoint.com/,Documents,DocLibrary_SubfolderName

通过加载一个JSON文件进行批量迁移的代码片段:

#Load JSON:
$jsonItems = Get-Content -Raw -Path  "C:\spmt.json" | ConvertFrom-Json
ForEach ($taskItem in $jsonItems.Tasks)
{
    $jsonString = ConvertTo-Json $taskItem -Depth 100
    Add-SPMTTask -JsonDefinition $jsonString -SharePointSourceCredential $onpremCredential
}

spmt.json文件中定义了三个迁移任务。

{
   "Tasks":[
      {
         "SourcePath":"http://On-prem/sites/test",
         "TargetPath":"https://YourSPO.sharepoint.com",
         "Items":{
            "Lists":[
               {
                  "SourceList":"list-01",
                  "TargetList":"list-01"
               }
            ],
            "SubSites":[

            ]
         }
      },
      {
         "SourcePath":"http://On-prem/sites/test",
         "TargetPath":"https://YourSPO.sharepoint.com",
         "Items":{
            "Lists":[
               {
                  "SourceList":"list-02",
                  "TargetList":"list-02"
               }
            ],
            "SubSites":[

            ]
         }
      },
      {
         "SourcePath":"http://On-prem/sites/test",
         "TargetPath":"https://YourSPO.sharepoint.com",
         "Items":{
            "Lists":[
               {
                  "SourceList":"doclib-01",
                  "TargetList":"doclib-01"
               }
            ],
            "SubSites":[

            ]
         }
      }
   ]
}

示例 4:显示迁移进度

这些示例展示如何显示迁移项目的进度。 Get-SPMTMigration 返回当前会话的对象。 它包括当前任务状态和当前会话级别设置。 当前任务的状态包括:

  • 已扫描文件数
  • 已筛选掉的文件数
  • 已迁移文件数
  • 失败文件数
  • 当前任务迁移进度 (0~100)
  • 当前任务状态
  • 迁移错误消息(如有)。

# Start migration in the background
Start-SPMTMigration -NoShow

# Get the object of current migration
$session = Get-SPMTMigration

# Query migration status every 5 seconds until migration is finished
while ($session.Status -ne "Finished")
{
Write-Host $session.Status

    # Query migration progress of each tasks
    Foreach ($taskStatus in $session.StatusOfTasks)
    {
        $taskStatus.MigratingProgressPercentage
}

    Start-Sleep -Seconds 5
}