尝试向SEPM API提交GET请求时请求错误

时间:2020-06-23 19:42:18

标签: powershell symantec

我正在尝试向SEPM API提交请求,特别是使用Symantec提供的文档中的this section,但是每次尝试运行查询时,都会出现(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"

1 个答案:

答案 0 :(得分:1)

provided document{startTime}{endTime}都应为 整数int64语法模式。不幸的是,-UFormat %s返回自1970年1月1日00:00:00 起经过的秒作为 double 值。进行适当的转换,例如如下:

$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