PowerShell 経由で OneDrive ファイルを別のユーザーに転送する方法

Powershell Jing Youde Onedrive Fairuwo Bienoyuzani Zhuan Songsuru Fang Fa



Microsoft OneDrive アカウントから別のユーザーへのファイルの転送は、OneDrive からコンテンツをダウンロードして、手動で他のアカウントにアップロードできるという意味で簡単です。この投稿では、その方法を紹介します PowerShell 経由で OneDrive ファイルを別のユーザーに転送する



  PowerShell 経由で OneDrive ファイルを別のユーザーに転送する方法





考慮すべき事項

OneDrive から別のアカウントにファイルをアップロードすることになると、現時点では 250 MB を超えるファイルをアップロードすることができないため、時間がかかる作業になります。幸いなことに、PowerShell はアップロードできないすべてのファイルをメモするため、それらのファイルを探して通常の方法で共有できます。





ファイルを他の OneDrive アカウントにアップロードする前に、ファイルはまずコンピューターにダウンロードされるため、先に進む前にハード ドライブまたは SSD に十分な空き領域があることを確認してください。また、インターネット接続が必要なため、全体的な転送速度はネットワークの品質によって異なります。



ここで、管理者アカウントには 2 要素認証が存在しないことに注意する必要があるため、この目的専用に 2FA を持たない一時的な管理者アカウントを作成します。

必要なもの

特別なスクリプトを使用して、ある OneDrive アカウントから別の OneDrive アカウントにファイルを移動します。したがって、スクリプトが問題なく動作するようにするには、今すぐ次の PowerShell モジュールをインストールしてください。

Outlookでリマインダーをオフにする方法

SharePoint PnP PowerShell モジュール



管理者として PowerShell ツールを開き、次のコマンドを実行します。

Install-Module SharePointPnPPowerShellOnline -Force

SharePoint Online 管理シェル

このツールの目的は、ユーザーの OneDrive アカウントのアクセス許可を変更することです。

から無料でダウンロードしてインストールします マイクロソフト.com

MSOnline V1 Powershell モジュール

この最後のモジュールをインストールするには、管理者として PowerShell で次のコマンドを実行してください。

2662C5CDD9B77388F2DE3F787​​ED3F794E9D46F20

OneDrive ファイルを別のアカウントに転送する方法

OneDrive アカウントから別のアカウントにファイルを転送するには、PowerShell を開いて、提供されたスクリプトを実行する必要があります。

PowerShellを開く

  Microsoft PowerShell 検索

Visual Studio Code または PowerShell を開きます。

これを行うには、[検索] ボタンをクリックして PowerShell を検索します。

そこから、アプリを右クリックし、ツールを管理者モードで開くように設計されたオプションを選択します。

スクリプトを実行する

  OneDrive PowerShell スクリプト

WindowsUpdateバッチファイル

次に、関連するスクリプトを実行する必要があります。記事の下部にあります。

スクリプトが非常に長いため、これを選択しました。

スクリプトを追加した後、キーボードの Enter キーを押します。

ファイルを転送する

最後に、ファイルを別の OneDrive アカウントに転送します。

Enter キーを押した直後に、電子メール アカウントを追加するように求められます。 退会するユーザーのユーザー名

また必要になります 宛先ユーザーのユーザー名 。これは、ファイルのコピー先および転送先となる OneDrive ユーザーです。

最後に、追加するように求められます Office 365 管理者のユーザー名

受信アカウントをチェックしてファイルが正しく転送されたかどうかを確認する前に、スクリプトが処理を実行するのを待ってください。

以下のスクリプトをコピーして貼り付けます。

$departinguser = Read-Host "Enter departing user's email"
$destinationuser = Read-Host "Enter destination user's email"
$globaladmin = Read-Host "Enter the username of your Global Admin account"
$credentials = Get-Credential -Credential $globaladmin
Connect-MsolService -Credential $credentials
$InitialDomain = Get-MsolDomain | Where-Object {$_.IsInitial -eq $true}
  
$SharePointAdminURL = "https://$($InitialDomain.Name.Split(".")[0])-admin.sharepoint.com"
  
$departingUserUnderscore = $departinguser -replace "[^a-zA-Z]", "_"
$destinationUserUnderscore = $destinationuser -replace "[^a-zA-Z]", "_"
  
$departingOneDriveSite = "https://$($InitialDomain.Name.Split(".")[0])-my.sharepoint.com/personal/$departingUserUnderscore"
$destinationOneDriveSite = "https://$($InitialDomain.Name.Split(".")[0])-my.sharepoint.com/personal/$destinationUserUnderscore"
Write-Host "`nConnecting to SharePoint Online" -ForegroundColor Blue
Connect-SPOService -Url $SharePointAdminURL -Credential $credentials
  
Write-Host "`nAdding $globaladmin as site collection admin on both OneDrive site collections" -ForegroundColor Blue
# Set current admin as a Site Collection Admin on both OneDrive Site Collections
Set-SPOUser -Site $departingOneDriveSite -LoginName $globaladmin -IsSiteCollectionAdmin $true
Set-SPOUser -Site $destinationOneDriveSite -LoginName $globaladmin -IsSiteCollectionAdmin $true
Write-Host "`nConnecting to $departinguser's OneDrive via SharePoint Online PNP module" -ForegroundColor Blue
Connect-PnPOnline -Url $departingOneDriveSite -Credentials $credentials
Write-Host "`nGetting display name of $departinguser" -ForegroundColor Blue
# Get name of departing user to create folder name.
$departingOwner = Get-PnPSiteCollectionAdmin | Where-Object {$_.loginname -match $departinguser}
# If there's an issue retrieving the departing user's display name, set this one.
 if  ($departingOwner -contains $null) {
    $departingOwner = @{
        Title = "Departing User"
    }
}
  
# Define relative folder locations for OneDrive source and destination
$departingOneDrivePath = "/personal/$departingUserUnderscore/Documents"
$destinationOneDrivePath = "/personal/$destinationUserUnderscore/Documents/$($departingOwner.Title)'s Files"
$destinationOneDriveSiteRelativePath = "Documents/$($departingOwner.Title)'s Files"
  
Write-Host "`nGetting all items from $($departingOwner.Title)" -ForegroundColor Blue
# Get all items from source OneDrive
$items = Get-PnPListItem -List Documents -PageSize 1000
$largeItems = $items | Where-Object {[long]$_.fieldvalues.SMTotalFileStreamSize -ge 261095424 -and $_.FileSystemObjectType -contains "File"}
 if  ($largeItems) {
    $largeexport = @()
     foreach  ($item  in  $largeitems) {
        $largeexport += "$(Get-Date) - Size: $([math]::Round(($item.FieldValues.SMTotalFileStreamSize / 1MB),2)) MB Path: $($item.FieldValues.FileRef)"
        Write-Host "File too large to copy: $($item.FieldValues.FileRef)" -ForegroundColor DarkYellow
    }
    $largeexport | Out-file C:\temp\largefiles.txt -Append
    Write-Host "A list of files too large to be copied from $($departingOwner.Title) have been exported to C:\temp\LargeFiles.txt" -ForegroundColor Yellow
}
$rightSizeItems = $items | Where-Object {[long]$_.fieldvalues.SMTotalFileStreamSize -lt 261095424 -or $_.FileSystemObjectType -contains "Folder"}
Write-Host "`nConnecting to $destinationuser via SharePoint PNP PowerShell module" -ForegroundColor Blue
Connect-PnPOnline -Url $destinationOneDriveSite -Credentials $credentials
Write-Host "`nFilter by folders" -ForegroundColor Blue
# Filter by Folders to create directory structure
$folders = $rightSizeItems | Where-Object {$_.FileSystemObjectType -contains "Folder"}
  
Write-Host "`nCreating Directory Structure" -ForegroundColor Blue
 foreach  ($folder  in  $folders) {
    $path = ('{0}{1}' -f $destinationOneDriveSiteRelativePath, $folder.fieldvalues.FileRef).Replace($departingOneDrivePath, '')
    Write-Host "Creating folder in $path" -ForegroundColor Green
    $newfolder = Ensure-PnPFolder -SiteRelativePath $path
}
  
Write-Host "`nCopying Files" -ForegroundColor Blue
$files = $rightSizeItems | Where-Object {$_.FileSystemObjectType -contains "File"}
$fileerrors = ""
foreach ($file in $files) {
    $destpath = ("$destinationOneDrivePath$($file.fieldvalues.FileDirRef)").Replace($departingOneDrivePath, "")
    Write-Host "Copying $($file.fieldvalues.FileLeafRef) to $destpath" -ForegroundColor Green
    $newfile = Copy-PnPFile -SourceUrl $file.fieldvalues.FileRef -TargetUrl $destpath -OverwriteIfAlreadyExists -Force -ErrorVariable errors -ErrorAction SilentlyContinue
    $fileerrors += $errors
}
$fileerrors | Out-File c:\temp\fileerrors.txt
# Remove Global Admin from Site Collection Admin role for both users
Write-Host "`nRemoving $globaladmin from OneDrive site collections" -ForegroundColor Blue
Set-SPOUser -Site $departingOneDriveSite -LoginName $globaladmin -IsSiteCollectionAdmin $false
Set-SPOUser -Site $destinationOneDriveSite -LoginName $globaladmin -IsSiteCollectionAdmin $false
Write-Host "`nComplete!" -ForegroundColor Green

スクリプトはここにあります レディットページ

読む : Windows の PowerShell で CSV をエクスポートする方法

PowerShell は OneDrive にアクセスできますか?

SharePoint Online PowerShell を使用すると、ユーザーは PowerShell ツールを使用して別の OneDrive アカウントに接続できるようになります。 PowerShell がコマンドレットを使用して OneDrive アカウントでの作業を開始するために、パスワードを入力するように求められます。

外部ユーザーも OneDrive にアクセスできますか?

外部ユーザーは OneDrive アカウントにアクセスできますが、これは許可されている場合に限ります。ユーザーはファイルに永久に、または一定期間アクセスできます。できることを制限することもできます。

他の人の OneDrive からファイルをコピーするにはどうすればよいですか?

他の人の OneDrive からファイルをコピーしたい場合は、次のオプションがあります。

  • リンクを使用してブラウザーで OneDrive を開き、コピーするファイルを選択して、[ダウンロード] をクリックします。これにより、コンピュータにダウンロードされます。
  • リンクを使用して OneDrive アカウントを開き、コピーするファイルを選択して、[コピー先] をクリックします。

それでおしまい!

  PowerShell 経由で OneDrive ファイルを別のユーザーに転送する方法
人気の投稿