Методы копирования файлов между семействами веб-сайтов SharePoint с помощью PowerShell

Чтобы скопировать файл из одного семейства сайтов SharePoint в другое с помощью PowerShell, вы можете использовать командную консоль SharePoint Online. Для этого можно использовать несколько методов. Вот несколько вариантов:

Метод 1. Командная консоль SharePoint Online

  1. Установите командную консоль SharePoint Online, если вы еще этого не сделали.
  2. Откройте PowerShell и подключитесь к SharePoint Online с помощью команды:
    Connect-SPOService -Url https://yourdomain-admin.sharepoint.com
  3. Скопируйте файл с помощью следующей команды:
    Copy-SPOFile -SourceUrl "https://source-site-collection-url/document-library/filename" -TargetUrl "https://target-site-collection-url/document-library/filename"

Метод 2: SharePoint PnP PowerShell

  1. Установите модуль SharePoint PnP PowerShell, если вы еще этого не сделали.
  2. Откройте PowerShell и подключитесь к SharePoint Online с помощью команды:
    Connect-PnPOnline -Url https://yourdomain.sharepoint.com/sites/source-site-collection
  3. Скопируйте файл с помощью следующей команды:
    Copy-PnPFile -SourceUrl "/sites/source-site-collection/document-library/filename" -TargetUrl "/sites/target-site-collection/document-library/filename"

Метод 3: CSOM (объектная модель на стороне клиента)

  1. Установите пакет SDK клиентских компонентов SharePoint Online, если вы еще этого не сделали.
  2. Создайте сценарий PowerShell и добавьте необходимый код CSOM для копирования файла. Вот пример кода:
    Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
    Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
    $sourceSiteUrl = "https://yourdomain.sharepoint.com/sites/source-site-collection"
    $targetSiteUrl = "https://yourdomain.sharepoint.com/sites/target-site-collection"
    $sourceFileUrl = "/sites/source-site-collection/document-library/filename"
    $targetFileUrl = "/sites/target-site-collection/document-library/filename"
    $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($sourceSiteUrl)
    $srcWeb = $ctx.Web
    $srcFile = $srcWeb.GetFileByServerRelativeUrl($sourceFileUrl)
    $ctx.Load($srcFile)
    $ctx.ExecuteQuery()
    $destinationUrl = $targetSiteUrl + $targetFileUrl
    $srcFileStream = $srcFile.OpenBinaryStream()
    $ctx.ExecuteQuery()
    $targetCtx = New-Object Microsoft.SharePoint.Client.ClientContext($targetSiteUrl)
    $targetWeb = $targetCtx.Web
    $targetFile = $targetWeb.GetFileByServerRelativeUrl($targetFileUrl)
    $targetCtx.Load($targetWeb)
    $targetCtx.ExecuteQuery()
    $uploadResult = Microsoft.SharePoint.Client.File.SaveBinaryDirect($targetCtx, $destinationUrl, $srcFileStream.Value, $true)
    $targetCtx.ExecuteQuery()

    Сохраните сценарий с расширением.ps1 и запустите его с помощью PowerShell.