当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 如何通过 PowerShell 将 OneDrive 文件传输给其他用户

作者:精品下载站 日期:2024-12-14 22:46:22 浏览:14 分类:玩电脑

如何通过 PowerShell 将 OneDrive 文件传输给其他用户


将文件从 Microsoft OneDrive 帐户传输到其他用户很容易,因为您可以从 OneDrive 下载内容,然后手动将其上传到其他帐户。在这篇文章中,我们将向您展示如何通过 PowerShell 将 OneDrive 文件传输给其他用户

[玩转系统] 如何通过 PowerShell 将 OneDrive 文件传输给其他用户

需要考虑的事项

当将文件从 OneDrive 上传到另一个帐户时,这是一项需要一些时间的任务,因为目前无法上传大于 250MB 的文件。好消息是 PowerShell 会记录所有无法上传的文件,因此您可以找到它们并通过常规方法共享它们。

在将文件上传到其他 OneDrive 帐户之前,文件将首先下载到您的计算机,因此请确保您的硬盘或 SSD 上有足够的空间,然后再继续。由于需要互联网连接,因此整体传输速度将取决于网络质量。

现在,我们必须注意,管理员帐户上不存在双因素身份验证,因此仅出于此目的创建一个没有 2FA 的临时管理员帐户。

你需要的东西

我们将使用特殊脚本将文件从一个 OneDrive 帐户移动到另一个帐户。因此,为了使脚本能够解决问题,请立即安装以下 PowerShell 模块:

SharePoint PnP PowerShell 模块

以管理员身份打开 PowerShell 工具,然后运行以下命令:

Install-Module SharePointPnPPowerShellOnline -Force

SharePoint Online Management Shell

该工具的目的是修改用户 OneDrive 帐户的权限。

从 microsoft.com 免费下载并安装它。

MSOnline V1 Powershell模块

为了安装这个最终模块,请以管理员身份在 PowerShell 中运行以下命令:

Install-Module MSOnline -Force

如何将 OneDrive 文件转移到另一个帐户

要将文件从您的 OneDrive 帐户传输到另一个帐户,您必须打开 PowerShell,然后运行提供的脚本。

打开 PowerShell

[玩转系统] 如何通过 PowerShell 将 OneDrive 文件传输给其他用户

打开 Visual Studio Code 或 PowerShell。

您可以通过单击“搜索”按钮,然后搜索 PowerShell 来执行此操作。

从那里,右键单击该应用程序,然后选择旨在以管理模式打开该工具的选项。

运行脚本

[玩转系统] 如何通过 PowerShell 将 OneDrive 文件传输给其他用户

接下来,您必须运行相关脚本。您可以在文章底部找到它。

我们选择这样做是因为脚本相当长。

添加脚本后,按键盘上的 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

您可以在此 Reddit 页面上找到该脚本。

PowerShell 可以访问 OneDrive 吗?

SharePoint Online PowerShell 将使用户可以使用 PowerShell 工具连接到另一个 OneDrive 帐户。它会要求您输入密码,以便 PowerShell 开始通过 cmdlet 处理您的 OneDrive 帐户。

外部用户可以访问 OneDrive 吗?

外部用户可以访问您的 OneDrive 帐户,但前提是您允许。用户可以永久或在设定的时间段内访问您的文件。您还可以限制他们可以做的事情。

如何从别人的OneDrive复制文件?

如果您想要从他人的 OneDrive 复制文件,您可以选择以下选项:

  • 使用链接在浏览器中打开 OneDrive,选择要复制的文件,然后单击“下载”。这会将其下载到您的计算机上。

  • 使用链接打开 OneDrive 帐户,选择要复制的文件,然后单击“复制到”。

就是这样!

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯