使用 Powershell 和 Graph Get-MgDriveItemContent 下载 OneDrive 文件

问题描述 投票:0回答:1

我正在尝试使用图形模块下载 OneDrive 文件。但我在最后一步遇到错误。我不知道如何继续。有人可以告诉我吗?

    Connect-MgGraph -Scopes "Directory.ReadWrite.All", "Sites.ReadWrite.All", "Files.ReadWrite.All"
#
# Import users from CSV
$terminatedUsers = Import-Csv -Path "C:\Users\me\Desktop\OneDriveUsersBackup.csv"

foreach ($user in $terminatedUsers) {
    $userPrincipalName = $user.UserPrincipalName
    
    # Create a directory for the user's data
    $userDirectory = "C:\TerminatedUsers\$userPrincipalName"
    New-Item -ItemType Directory -Path $userDirectory -Force

    # Fetch user information
    $userObject = Get-MgUser -Filter "userPrincipalName eq '$userPrincipalName'"

    # Fetch user's OneDrive drives
    $userOneDrive = Get-MgUserDefaultDrive -UserId $userObject.id


        $driveId = $userOneDrive.id
        #Get-MgUserDriveItemContent -UserId $userObject.id -DriveId $driveId -DriveItemId "root" -OutFile $userDirectory
        Get-MgDriveItemContent -DriveId $driveId -DriveItemId "root" -OutFile $userDirectory
    
}
azure powershell microsoft-graph-api
1个回答
0
投票

最初,当我通过在 Get-MgDriveItemContent 命令中传递“root”作为

-DriveItemId
的值来运行代码时,我也遇到了同样的错误

Get-MgDriveItemContent -DriveId $driveId -DriveItemId "root" -OutFile $userDirectory

回复:

enter image description here

当您尝试直接从驱动器的 root 获取内容而未指定有效的

DriveItemId
时,发生了错误。

我有一个名为 Sri 的用户帐户,One Drive 帐户中包含以下文件:

enter image description here

要使用 Microsoft Graph PowerShell 下载这些 OneDrive 文件,您可以使用以下修改后的脚本:

Connect-MgGraph -Scopes "Directory.ReadWrite.All", "Sites.ReadWrite.All", "Files.ReadWrite.All"

# Import users from CSV
$terminatedUsers = Import-Csv -Path "C:\Users\me\Desktop\OneDriveUsersBackup.csv"

foreach ($user in $terminatedUsers) {
    $userPrincipalName = $user.UserPrincipalName
    
    # Create a directory for the user's data
    $userDirectory = "C:\TerminatedUsers\$userPrincipalName"
    New-Item -ItemType Directory -Path $userDirectory -Force

    # Fetch user information
    $userObject = Get-MgUser -Filter "userPrincipalName eq '$userPrincipalName'"

    # Fetch user's OneDrive drives
    $userOneDrive = Get-MgUserDefaultDrive -UserId $userObject.id
    $driveId = $userOneDrive.id

    # Fetch root drive item
    $rootDriveItem = Get-MgDriveItem -DriveId $driveId -DriveItemId "root"

    # Fetch children of the root drive item
    $driveItemChildren = Get-MgDriveItemChild -DriveId $driveId -DriveItemId $rootDriveItem.id

    # Loop through each drive item
    foreach ($item in $driveItemChildren) {
        # Check if the item is a file based on MIME type
        if ($item.File.MimeType -ne $null) {
            $fileDirectory = $userDirectory
            $fileName = $item.name

            # Download the file
            Get-MgDriveItemContent -DriveId $driveId -DriveItemId $item.id -OutFile (Join-Path -Path $fileDirectory -ChildPath $fileName)
        } else {
            # If it's not a file, it could be a folder, we can skip it
            continue
        }
    }
}

回复:

enter image description here

为了确认这一点,我检查了 OneDrive 文件下载成功的路径,如下所示:

enter image description here

在下载文件夹的情况下,您应该枚举文件夹中的每个对象,将它们放入一个数组中,然后尝试像评论中提到的那样下载它们。

© www.soinside.com 2019 - 2024. All rights reserved.