首页 > 解决方案 > 通过使用筛选器的 API 调用从 Microsoft Defender ATP 仅返回计算机子集的问题

问题描述

我正在尝试按照 Microsoft 的说明(https://docs.microsoft.com/en-us/windows/security/threat-protection/microsoft- )使用 OData $filter 查询通过 API 调用从 Microsoft Defender ATP 获取机器子集defer-atp/exposed-apis-odata-samples),但无论我做什么,我都会得到相同的全套机器,限制在 10,000 台。

因此,出于某种原因,我的以下代码无法正常工作。我究竟做错了什么?另外,我如何获得超过10,000台机器?

我从下面的代码中删除了tenantId、appId 和 appSecret 变量。

更新: 我注意到,当我在运行脚本后检查 PowerShell ISE 中的 $machinesUrl2 变量的值时,它显示URI 中缺少“ $filter ”。以下是变量 $machinesUrl2 的输出:

https://api.securitycenter.windows.com/api/machines?=healthStatus+eq+'Inactive'

是什么导致“ $filter ”下降?这是正常行为吗?

谢谢,

$resourceAppIdUri = 'https://securitycenter.onmicrosoft.com/windowsatpservice'
$oAuthUri = "https://login.windows.net/$TenantId/oauth2/token"
$authBody = [Ordered] @{
    resource = "$resourceAppIdUri"
    client_id = "$appId"
    client_secret = "$appSecret"
    grant_type = 'client_credentials'
}
$authResponse = Invoke-RestMethod -Method Post -Uri $oAuthUri -Body $authBody -ErrorAction Stop
$aadToken = $authResponse.access_token

$machinesUrl2 =  "https://api.securitycenter.windows.com/api/machines?$filter=healthStatus+eq+'Inactive'"

$headers = @{ 
    'Content-Type' = 'application/json'
    Accept = 'application/json'
    Authorization = "Bearer $aadToken"
}

$machinesResponse = Invoke-WebRequest -Method Get -Uri $machinesUrl2 -Headers $headers -ErrorAction Stop
$machines =  ($machinesResponse | ConvertFrom-Json).value

标签: powershellmicrosoft-graph-apiodatawindows-defender

解决方案


我在试图弄清楚为什么$filter从查询字符串中删除时找到了答案。

它需要在“ $filter ”前面添加一个反引号字符(` ) 。

https://api.securitycenter.windows.com/api/machines?`$filter=healthStatus+eq+'Inactive'

将此字符添加到查询字符串后,此 Microsoft 文档中的代码开始工作。


推荐阅读