使用 PowerShell 移轉至 SharePoint

本文內容涉及以 SharePoint 移轉工具 (SPMT) 移轉引擎為基礎的 PowerShell Cmdlet。 可用於將檔案從 SharePoint Server 內部部署文件庫和清單項目以及檔案共用移動到 Microsoft 365。 如需有關所有目前與 SharePoint 移轉相關的 PowerShell Cmdlet 資訊,請參閱 Microsoft SharePoint 移轉工具 Cmdlet 參考。

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

注意事項

這些 PowerShell Cmdlet 目前無法供中國由 21Vianet 運作的 Office 365 使用者使用。

系統需求

描述 建議
CPU 64 位元 4 核心處理器或更佳版本
RAM 16 GB
本機儲存空間 硬碟:150 GB 的可用空間
作業系統 Windows Server 2025 Standard 或 DatacenterWindows
Server 2022 Standard 或 DatacenterWindows
Server 2019 Standard 或Datacenter
Windows Server 2016 Standard 或 Datacenter
Windows Server 2012 R2 Standard 或 Datacenter
Windows 10或 11 用戶端
.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 或 2012
Windows 7、8 或 8.1
.NET Framework 4.6.2
Microsoft Visual C++ 2015 可轉散發套件 OneNote 移轉需求。
PowerShell 需要 PowerShell 5.x 才能支援最多 400 個字元的檔案路徑移轉。 不支援 PowerShell 6.0 或更新版本。

長路徑問題的因應措施

使用 SPMT PowerShell 時,您可能會在路徑超過 260 個字元的來源檔案上遇到掃描失敗。 若要解決此問題,您可以修改系統登錄,讓 PowerShell 能夠讀取長路徑檔案。

開啟 [註冊編輯器],瀏覽至 "Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework",並新增機碼 "AppContext"。 然後在機碼底下新增 2 個字串值。

  • 數值名稱: Switch.System.IO.BlockLongPaths 數值資料: false

  • 數值名稱: Switch.System.IO.UseLegacyPathHandling 數值資料: false

開始之前

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

  2. 開啟資料夾: $env:UserProfile\Documents\WindowsPowerShell\Modules\Microsoft.SharePoint.MigrationTool.PowerShell。 請確定您有 DLL。 如果您使用的是 OneDrive,則需要將 WindowsPowershell 資料夾複製到 $env:UserProfile\OneDrive - <Organization Name>\Documents。

  3. 請從這個位置執行下列 PowerShell 命令:

    Import-Module Microsoft.SharePoint.MigrationTool.PowerShell
    

建立和初始化移轉工作階段

此 Cmdlet 會建立並初始化移轉工作階段。 初始化會在工作階段層級設定移轉設定。 如果未定義特定的設定參數,則使用預設設定。 註冊工作階段後,您可以將工作添加到工作階段並開始遷移。

新增移轉工作

使用此 Cmdlet 可將新的移轉工作新增至已註冊的移轉工作階段。 目前允許三種不同類型的工作:檔案共用工作、SharePoint 工作和 JSON 定義工作。

注意:不允許重複的工作。

移除工作

使用此 Cmdlet 從註冊的移轉中移除現有的移轉工作。

開始移轉

此 Cmdlet 會啟動已註冊的 SPMT 移轉。

傳回目前工作階段的物件

傳回目前工作階段的物件。 此 Cmdlet 包含目前工作的狀態和目前工作階段層級設定。 目前的工作狀態包括:

  • 掃描檔案的數目
  • 已移轉檔案的數目
  • 任何移轉錯誤訊息

停止目前的移轉

此 Cmdlet 會取消目前的移轉。

在主控台中顯示移轉狀態詳細資料

  • Show-SPMTMigration

如果您以 NoShow 模式開始移轉,執行 Show-SPMTMigration Cmdlet 會在主控台中顯示工作識別碼、資料來源位置、目標位置及移轉狀態。 選取 Ctrl+C,顯示器會返回 NoShow 模式。

移除移轉工作階段

使用此 Cmdlet 刪除移轉工作階段。

範例案例

範例 1: IT 系統管理員新增 SharePoint 內部部署工作,並在背景中開始移轉。

#Define SharePoint Server 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 $SPCredential
}

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
}