【发布时间】:2021-11-04 04:09:24
【问题描述】:
我正在使用 Microsoft 365 Defender API 来接收所有最近的事件/事件。
我得到一个json文件如下:link to example json
并使用以下脚本尝试将其转换为轻松导入 SQL 服务器: (仅作为测试回显)
# Send the request and get the results.
$response = Invoke-WebRequest -UseBasicParsing -Method Get -Uri $url -Headers $headers -ErrorAction Stop
# Extract the incidents from the results.
$alerts = ($response | ConvertFrom-Json)
$devices = ($response | ConvertFrom-Json ).value.alerts.devices
$entities = ($response | ConvertFrom-Json ).value.alerts.entities
Foreach($row in $alerts){
$IncidentID = $alerts.value.incidentID
$Createdtime = $alerts.value.creationTime
$Status = $alerts.value.status
$Severity = $alerts.value.severity
$Classification = $alerts.value.classification
$IncidentName = $alerts.value.incidentName
$URL = $alerts.incidentUri
$Klant = $afkorting
$Username = $entities.accountname
$device = $devices.deviceDnsName
echo $IncidentID
echo $Createdtime
echo $Status
echo $Severity
echo $Classification
echo $IncidentName
echo $URL
echo $Klant
echo $Username
echo $device
Invoke-Sqlcmd -ServerInstance "SQL.domain.local\MSQL2016" -Database "private" -Username private -Password 'private' -Query "INSERT Into dbo.private ( [IncidentID], [Createdtime], [Status], [Severity], [Classification], [IncidentName], [URL], [Klant], [Username], [device]) VALUES ('$IncidentID', '$Createdtime', '$Status', '$Severity', '$Classification', '$IncidentName', '$URL', '$Klant', '$Username', '$device')"
}
但是,3 次事件的输出如下所示:
事件ID
事件ID
事件ID
创建时间
创建时间
创建时间
状态
状态
状态
所以按元素分组,而不是按 IncidentID 分组。
我找不到获得如下输出的方法:
事件ID
创建时间
状态
严重性
分类
事件名称
网址
克兰特
用户名
设备
我现在通过一个中间步骤“解决”了这个问题,该步骤导出到 CSV 并合并它们并将它们通过管道传输到 SQL,但这太低效了。
【问题讨论】:
标签: sql json powershell type-conversion