powershellsymantec

Bad request when trying to submit a GET request to SEPM API


I'm trying to submit a request to SEPM API, specifically using this section of the docs provided by Symantec, but any time I try to run the query I get the (400) Bad request error, and to be honest I don't see the malformation on the request. This is the code I'm using so far:

#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

The Uri expands as follows "https://example:8446/sepm/api/v1/stats/client/malware/Hour/1592937124.87678/to/1592940724.8778"


Solution

  • The provided document says that both {startTime} and {endTime} should be integer (int64) in the given syntax pattern. Unfortunately, -UFormat %s returns seconds elapsed since January 1, 1970 00:00:00 as a double value. Apply appropriate conversion, e.g. as follows:

    $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]
    

    Note that you could simplify the code having in mind that both following expressions evaluate to true:

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