问题描述
我试图按照Microsoft的说明(https://docs.microsoft.com/en-us/windows/security/threat-protection/microsoft-defender-atp/exposed-apis-odata-samples)使用OData $ filter查询通过API调用从Microsoft Defender ATP中获得部分计算机,但是无论我做什么,我都会得到相同的全套机器的数量限制为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
解决方法
我试图找出为什么 $ filter 从查询字符串中删除的原因找到了答案。
它需要在“ $ filter ”前面添加反引号(`)。
https://api.securitycenter.windows.com/api/machines?`$filter=healthStatus+eq+'Inactive'
将此字符添加到查询字符串后,此Microsoft文档上的代码开始起作用。