首页 > 解决方案 > 尝试向 SEPM API 提交 GET 请求时出现错误请求

问题描述

我正在尝试向 SEPM API 提交请求,特别是使用赛门铁克提供的文档的这一部分,但是每当我尝试运行查询时,我都会收到(400) Bad request错误消息,老实说,我没有看到格式错误要求。这是我到目前为止使用的代码:

#Report type: Hour, Day, Week or Month
$reportType = "Hour"

#Get system time
$startTime = Get-Date (Get-Date).AddHours(-1)
$endTime =  Get-Date

#Convert time to Epoch
$startTimeEpoch = Get-Date ($startTime).ToUniversalTime() -UFormat %s
$endTimeEpoch = Get-Date ($endTime).ToUniversalTime() -UFormat %s

$header = @{
    "Content-Type" = "application/json"
    "Authorization"= "Bearer $authToken"
}

$response = Invoke-RestMethod `
    -Uri "https://example:8446/sepm/api/v1/stats/client/malware/$reportType/$startTimeEpoch/to/$endTimeEpoch" `
    -Method GET `
    -Headers $header

Uri 扩展如下"https://example:8446/sepm/api/v1/stats/client/malware/Hour/1592937124.87678/to/1592940724.8778"

标签: powershellsymantec

解决方案


提供的文档说在给定的语法模式中两者{startTime}{endTime}都应该是整数( int64)。不幸的是,-UFormat %s返回自 1970 年 1 月 1 日 00:00:00 以来经过的秒数作为精度值。应用适当的转换,例如如下:

$reportType = "Hour"

#Get system time
$endTime   = Get-Date
$startTime = $endTime.AddHours(-1)

#Convert time to Epoch
$startTimeEpoch = (Get-Date ($startTime).ToUniversalTime() -UFormat %s).
                    ToDouble([cultureinfo]::CurrentCulture) -as [int64]
$endTimeEpoch   = (Get-Date (  $endTime).ToUniversalTime() -UFormat %s).
                    ToDouble([cultureinfo]::CurrentCulture) -as [int64]

请注意,您可以简化代码,同时记住以下两个表达式的计算结果均为true

$startTimeEpoch -eq $endTimeEpoch -3600
$startTimeEpoch +3600 -eq $endTimeEpoch

推荐阅读