Microsoft graph api 根据类别颜色列出消息计数 returns 使用 PowerShell 时不正确的 odata 计数

Microsoft graph api list message count based on category color returns incorrect odata count when using PowerShell

我正在使用 PowerShell 脚本根据应用于消息的类别颜色检索消息计数。我正在使用 Microsoft Graph api 来实现这一点。当我使用 Microsoft online graph api 开发人员工具测试请求端点时,我得到了 odata.count 的正确值。但是,当我使用 powershell 查询相同的端点 uri 时,我得到 odata.count 的错误值(这里是该特定文件夹中所有消息的端点 returns 总数)。

以下是测试的片段。 使用在线 Microsoft Graph api 开发人员工具查询端点

工具的响应(这是所有类别为绿色的消息的真实值)

使用powershell查询同一个graph uri时的请求和响应值(这是不正确的值)

下面是请求调用端点

 $accessToken = "accesstokenhere"
 $mail_user = "emailhere"
 $blue_uri = "https://graph.microsoft.com/v1.0/users/$mail_user/mailFolders/inbox/messages?$filter=categories/any(a:a+eq+'Green+category')&count=true"
    
 $blue_resp = Invoke-RestMethod -Method get -Uri $blue_uri -ContentType "application/json" -Headers @{Authorization=("bearer {0}" -f $accessToken)}
    
 $blue_resp

已在我用于获取访问令牌的 azure ad oauth 应用程序中分配了所有 Microsoft Graph api mail and mailboxsettings 权限。

有什么问题吗?

PowerShell 期望 $filter 是一个变量。您需要在 $filter

之前使用反引号转义字符 `
$blue_uri = "https://graph.microsoft.com/v1.0/users/$mail_user/mailFolders/inbox/messages?`$filter=categories/any(a:a+eq+'Green+category')&count=true"