使用Microsoft Graph中的报告时遇到问题 - getOneDriveUsageAccountDetail

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

我正在尝试从PowerShell中为getOneDriveUsageAccountDetail REST GET提取数据。麻烦的是,默认情况下它只会返回200个项目,我已经设法让它返回10000个项目,我认为这是一个硬限制。但我需要获取所有项目,或者能够查询一个用户或一批用户的报告。这似乎不可能。

我发现我可以将&%24top=10000添加到我的URI中以返回10000个结果。

https://docs.microsoft.com/en-us/graph/api/reportroot-getonedriveusageaccountdetail?view=graph-rest-1.0

唯一的参数是期间和日期。

我有一个Initialize-Authorization函数来创建$ script:APIHeader来存储访问令牌。这很好用。

这是我用来生成报告的功能。

Function Get-GraphOneDriveUsageAccountDetail {
    $result = (Invoke-RestMethod `
            -Method get `
            -Uri "https://graph.microsoft.com/beta/reports/getOneDriveUsageAccountDetail(period='D180')?%24format=application%2Fjson&%24top=10000" `
            -ContentType 'application/json' `
            -Headers $script:APIHeader `
            -ErrorAction Stop).value
    return $result
}
rest powershell microsoft-graph
2个回答
0
投票

大多数Graph端点返回paged data

一些针对Microsoft Graph的查询由于服务器端分页或由于使用$top查询参数来特定地限制请求中的页面大小而返回多页数据。当结果集跨越多个页面时,Microsoft Graph会在响应中返回@odata.nextLink属性,该属性包含指向下一页结果的URL。

你需要在结果中跟随nextLink来检索下一页(最后一页是没有nextLink的第一个结果)。


0
投票

这是我的最终代码:

Function Get-GraphOneDriveUsageAccountDetail {
    $AccountDetail = Invoke-RestMethod `
            -Method get `
            -Uri "https://graph.microsoft.com/beta/reports/getOneDriveUsageAccountDetail(period='D180')?%24format=application%2Fjson&%24top=10000" `
            -ContentType 'application/json' `
            -Headers $script:APIHeader `
            -ErrorAction Stop
            $result = $AccountDetail.value
While($AccountDetail.'@odata.nextLink')
    {
            Write-Verbose "Next Link $($AccountDetail.'@odata.nextLink')" -Verbose
            $AccountDetail = Invoke-RestMethod `
            -Method get `
            -Uri $AccountDetail.'@odata.nextLink' `
            -ContentType 'application/json' `
            -Headers $script:APIHeader `
            -ErrorAction Stop
            $result += $AccountDetail.value
    }
    return $result
}
© www.soinside.com 2019 - 2024. All rights reserved.