使用 PowerShell 移轉至 SharePoint

本文是關於以 SharePoint 移轉工具 (SPMT) 移轉引擎為基礎的新 PowerShell Cmdlet。 它們可用來將檔案從 SharePoint 2010 和 SharePoint 2013 內部部署文件庫和清單專案,以及檔案共用移至 Microsoft 365。 如需與 SharePoint 移轉相關的所有目前 PowerShell Cmdlet 相關資訊,請參閱 Microsoft SharePoint 移轉工具 Cmdlet 參考

PowerShell Cmdlet 提供與 SharePoint 移轉工具相同的功能。

注意事項

這些 PowerShell Cmdlet 目前不適用於由中國的 21Vianet 運作的 Office 365 使用者。

系統需求

最佳效能的建議需求

描述 建議
CPU 64 位元四核心或更高等級處理器
RAM 16 GB
本機儲存空間 硬碟:150 GB 的可用空間
作業系統 Windows Server 2016 Standard 或 Datacenter
Windows Server 2012 R2
Windows 10 用戶端
.NET Framework 4.6.2
Microsoft Visual C++ 2015 可轉散發套件 OneNote 移轉需求

重要事項

需要 PowerShell 5.0x 和 .NET Framework 4.6.2 或更新版本,才能支援最多 400 個字元的檔案路徑移轉。

最低需求 (效能較低)

描述 基本需求
CPU 64 位元 1.4 GHz 雙核心或更高等級處理器
RAM 8 GB
本機儲存空間 硬碟:150 GB 的可用空間
網路卡 高速網際網路連線
作業系統 Windows Server 2008 R2
Windows 7
Windows 8 或 8.1
.NET Framework 4.6.2
Microsoft Visual C++ 2015 可轉散發套件 OneNote 移轉需求。
PowerShell 支援最多 400 個字元之檔案路徑移轉所需的 PowerShell 5.x。 不支援 PowerShell 6.0 或更新版本。

開始之前

  1. 使用現有的 Active Directory 或其中一個其他選項來布建 Microsoft 365,以將帳戶新增至 Microsoft 365。 如需詳細資訊,請參閱Microsoft 365 與內部部署環境整合將使用者新增至Microsoft 365 Apps 商務版

  2. 開啟資料夾:

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

    請確認當中有 DLL,如果您使用的是 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 會在主控台中顯示工作識別碼、資料來源位置、目標位置和移轉狀態。 按 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
}