如何通過 PowerShell 將 OneDrive 文件傳輸給另一個用戶

如何通過 PowerShell 將 OneDrive 文件傳輸給另一個用戶

將文件從您的 Microsoft OneDrive 帳戶傳輸到另一個用戶很容易,因為您可以從您的 OneDrive 下載內容,然後手動將它們上傳到另一個帳戶。在這篇文章中,我們將向您展示如何通過 PowerShell 將 OneDrive 文件傳輸給另一個用戶

需要考慮的事情

當涉及到將文件從您的 OneDrive 上傳到另一個帳戶時,這是一項需要一些時間的任務,因為目前無法上傳大於 250MB 的文件。好消息是 PowerShell 會記錄它無法上傳的所有文件,因此您可以找到它們並通過常規方法共享它們。

在將文件上傳到其他 OneDrive 帳戶之前,文件將首先下載到您的計算機,因此請確保您的硬盤驅動器或 SSD 上有足夠的空間,然後再繼續。由於需要互聯網連接,因此整體傳輸速度將取決於網絡質量。

現在,我們必須注意管理員帳戶不存在雙因素身份驗證,因此創建一個沒有 2FA 的臨時管理員帳戶僅用於此目的。

你需要的東西

我們將使用特殊腳本將文件從一個 OneDrive 帳戶移動到另一個帳戶。因此,為了使腳本能夠正常工作,請立即安裝以下 PowerShell 模塊:

SharePoint PnP PowerShell 模塊

以管理員身份打開 PowerShell 工具,然後運行以下命令:

Install-Module SharePointPnPPowerShellOnline -Force

SharePoint 在線管理外殼

此工具的用途是修改用戶 OneDrive 帳戶的權限。

從microsoft.com免費下載並安裝它。

MSOnline V1 Powershell 模塊

為了安裝這個最終模塊,請以管理員身份在 PowerShell 中運行以下命令:

Install-Module MSOnline -Force

如何將 OneDrive 文件傳輸到另一個帳戶

要將文件從您的 OneDrive 帳戶傳輸到另一個帳戶,您必須打開 PowerShell,然後運行提供的腳本。

打開 PowerShell

微軟 PowerShell 搜索

打開 Visual Studio Code 或 PowerShell。

您可以通過單擊“搜索”按鈕,然後搜索 PowerShell 來執行此操作。

從那裡,右鍵單擊該應用程序,然後選擇旨在以管理員模式打開該工具的選項。

運行腳本

OneDrive PowerShell 腳本

接下來,您必須運行相關腳本。您可以在文章底部找到它。

我們選擇這樣做是因為腳本很長。

添加腳本後,按鍵盤上的 Enter 鍵。

傳輸文件

最後,現在是時候將文件傳輸到另一個 OneDrive 帳戶了。

你看,在按下 Enter 鍵後,你將被要求添加電子郵件帳戶The username of the departing user

您還需要目標用戶的用戶名。這是文件將被複製和傳輸到的 OneDrive 用戶。

最後,系統會要求您添加The username of your Office 365 Admin

在檢查接收帳戶以查看文件是否已正確傳輸之前,等待腳本完成它的工作。

複製並粘貼以下腳本:

$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

您可以在此Reddit 頁面上找到該腳本。

PowerShell 可以訪問 OneDrive 嗎?

SharePoint Online PowerShell 將使用戶能夠使用 PowerShell 工具連接到另一個 OneDrive 帳戶。它會要求您輸入密碼,以便 PowerShell 開始通過 cmdlet 處理您的 OneDrive 帳戶。

外部用戶可以訪問 OneDrive 嗎?

外部用戶可以訪問您的 OneDrive 帳戶,但前提是您允許。用戶可以永久或在設定的時間段內訪問您的文件。您還可以限制他們可以做什麼。

如何從別人的 OneDrive 複製文件?

如果您想要從其他人的 OneDrive 複製文件,您有以下選項:

  • 使用鏈接在瀏覽器中打開 OneDrive,選擇要復制的文件,然後單擊“下載”。這會將其下載到您的計算機。
  • 使用鏈接打開 OneDrive 帳戶,選擇要復制的文件,然後單擊“複製到”。

就是這樣!

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *