【发布时间】:2020-11-13 16:08:56
【问题描述】:
我刚开始使用 REST API,我唯一可用的语言是 PowerShell
我正在调用“事物”列表 - 这有效,我得到了 ID 属性
然后我尝试遍历该列表并获取“事物”(第二个 Invoke-RestMethod)的更详细记录 - 当我尝试这样做时,我得到一个错误:
Invoke-RestMethod : {"reply": {"err_code": 500, "err_msg": "处理 XDR 公共 API 时输入无效", "err_extra": "request_data 必须是类型:dict"}}
我认为 convertto-json 会将哈希表“请求”转换为字典。
我是新手,所以请不要以为我什么都知道。
$ContentType = "application/json"
$URL_All = 'https://api-<company>.xdr.eu.paloaltonetworks.com/public_api/v1/endpoints/get_endpoints/'
$URLEndpoint = 'https://api-<company>.xdr.eu.paloaltonetworks.com/public_api/v1/endpoints/get_endpoint/'
$endpoints = Invoke-RestMethod -uri $URL_All -method POST -Headers @{"x-xdr-auth-id" = "1"; "Authorization" = "<API-KEY_Goes_Here>" } -ContentType "application/json" -Verbose
if ($endpoints.reply.count -gt 0){
write-host "Found $($endpoints.reply.count) details"
}
else {
write-host " API didn't return any endpoint - ending script "
break
}
foreach ($endpoint in $endpoints.reply) {
if($endpoint.agent_id -ne $null){
$request = @{
request_data = '{}'
filters = @{
field = "endpoint_id_list"
operator = "in"
Value = "$($endpoint.agent_ID)"
Search_from = 0
Search_to = 1
sort = @{
field = "last_seen"
keyword = "desc"
}
}
} | ConvertTo-Json
$Endpoint_full += Invoke-RestMethod -uri $URLEndpoint -method POST -Body $request -Headers @{"x-xdr-auth-id" = "1"; "Authorization" = "<API-KEY_Goes_Here>" } -ContentType "application/json" -Verbose
}
}
【问题讨论】:
-
@iRon 总是值得一看,在这种情况下,它使用默认深度 2 转换为 Json 并返回没有问题。
标签: powershell rest