You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Microsoft Graph API批量请求licenseDetails无返回值问题咨询

问题

单独调用/users/{id}/licenseDetails端点可正常返回用户许可详情,但通过Graph API的$batch批量请求时,虽返回200状态码,但value字段为空。

需获取用户分配的订阅详情(含许可下各应用及状态),但licenseDetails不支持通过/users端点的$expand扩展获取,/users返回的其他属性也不包含该类信息。因要获取租户所有用户的相关数据,逐个调用API效率过低,故尝试每次批量组合20个请求,使用PowerShell的Invoke-RestMethod实现,代码如下:

for($i=0;$i -lt $data.count;$i+=20){                                                                                                                                              
    $req = @{}                                                     
    $req['requests'] = ($data[$i..($i+19)] 
        | select @{n='id';e={$_.id}},@{n='method';e={'GET'}},`
        @{n='url';e={"/users/$($_.id)/licenseDetails"}})
    $response = Invoke-RestMethod -Method POST `
        -URI "https://graph.microsoft.com/v1.0/`$batch" `
        -body ($req | convertto-json) `
                -Headers $HeaderParams
    foreach ($bat in $response.responses) {                                    
        if($bat.status -eq 200){
        write-host "$($bat.body)"
        } else {
            write-host "failed"
        }                                                      
    }                                                                                                                                              
}

返回结果中,$response.responses.body显示@{@odata.context=https://graph.microsoft.com/v1.0/$metadata#users('99ce89e8-7803-42a2-a387-fbc0bb60db70')/licenseDetails; value=System.Object[]},但所有$response.responses.body.value均为空。

询问是否有人成功实现licenseDetails的批量请求,或有无无需逐个调用即可获取租户所有用户许可详情的方法。

解决方法

1. 修复批量请求代码问题

你的批量请求存在两个关键问题:

  • 批量请求的id字段应为子请求的唯一标识(如序号字符串),而非用户ID。Graph API用该字段匹配请求与响应,使用用户ID会导致处理异常。
  • ConvertTo-Json默认深度不足,可能导致嵌套对象序列化不完整。

修改后的代码如下:

for($i=0;$i -lt $data.count;$i+=20){
    $batchRequests = @()
    $requestId = 1
    foreach ($user in $data[$i..($i+19)] | Where-Object { $_ -ne $null }) {
        $batchRequests += [PSCustomObject]@{
            id = [string]$requestId++
            method = 'GET'
            url = "/users/$($user.id)/licenseDetails"
        }
    }
    $req = @{ requests = $batchRequests }
    $response = Invoke-RestMethod -Method POST `
        -URI "https://graph.microsoft.com/v1.0/`$batch" `
        -Body ($req | ConvertTo-Json -Depth 10) `
        -Headers $HeaderParams
    foreach ($bat in $response.responses) {
        if($bat.status -eq 200){
            Write-Host ($bat.body | ConvertTo-Json -Depth 10)
        } else {
            Write-Host "请求ID $($bat.id) 失败"
        }
    }
}

2. 使用Microsoft Graph PowerShell模块简化操作

直接使用官方模块的Get-MgUserLicenseDetail命令,无需手动编写批量请求,支持高效批量获取:

# 安装并导入模块(首次使用)
Install-Module Microsoft.Graph.Users -Force
Import-Module Microsoft.Graph.Users

# 获取所有用户ID
$users = Get-MgUser -Select Id

# 批量获取许可详情
$allLicenseDetails = $users | ForEach-Object {
    Get-MgUserLicenseDetail -UserId $_.Id
}

若需更高效率,可使用并行处理:

$allLicenseDetails = $users | ForEach-Object -Parallel {
    Import-Module Microsoft.Graph.Users
    Get-MgUserLicenseDetail -UserId $_.Id
} -ThrottleLimit 15

3. 报表API替代方案

若仅需租户整体的许可分配统计或批量数据,可使用Graph报表API,无需逐个用户调用:

  • 获取Office 365活跃用户许可详情:GET /reports/getOffice365ActiveUserDetail
  • 获取许可使用详情:GET /reports/getLicenseUsageDetail

内容的提问来源于stack exchange,提问作者bstetson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 17:37:23