powershell - 尝试向 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"
解决方案
提供的文档说在给定的语法模式中两者{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
推荐阅读
- java - 我们如何在 PriorityQueue 类的对象中创建函数(比较)。请用 Java 解释这个语法
- php - 块的 phpword 库克隆正在本地主机上工作,但不适用于生产
- xcode - Xcode 在标签上设置文本会导致按钮功能不起作用
- web-services - WSDL 服务占位符解释
- oracle - ORA-20900: 目录 MY_DIR 不存在或没有权限
- reactjs - componentDidMount 和 componentDidUpdate 一起使用 useEffect 钩子
- ios - 无法将点击手势识别器添加到 SwiftUI MKMapView UIViewRepresentable
- reactjs - 没有重载将此调用与样式化组件匹配 (index.d.ts(2187, 9))
- ruby-on-rails - 必须使用 attr_encrypted 指定 iv 错误
- c# - 在.net项目中解析来自http post async的结果时出错