【问题标题】:Multi-level json to SQL outputs per element instead of per row多级 json 到 SQL 输出每个元素而不是每行
【发布时间】: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


    【解决方案1】:

    $devices$entities 的分辨率移动到循环中,然后使用迭代器变量$row 而不是在循环体内引用all $alerts

    # 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)
    
    foreach($row in $alerts){
      $IncidentID     = $row.value.incidentID
      $Createdtime    = $row.value.creationTime
      $Status         = $row.value.status
      $Severity       = $row.value.severity
      $Classification = $row.value.classification
      $IncidentName   = $row.value.incidentName
      $URL            = $row.incidentUri
      $Klant          = $afkorting              # where does `$afkorting` come from?
      $Username       = $row.value.entities.accountname
      $device         = $row.value.devices.deviceDnsName
    
      # Insert into SQL Server here
    }
    

    【讨论】:

    • 不幸的是,这有同样的问题,SQL 表现在显示在 IncidentID 列的第 1 行:23 15 22 21 20 19 18 17 16 13 14AKA 所有值都在 1 行中,而不是每个 ID 中的 1 行。 $afkorting 在 $response 之前定义,其中包含一个客户 ID。
    • @Sander 那么您链接的示例 JSON 不完整(父级属性 value 似乎至少被省略了?),请发布实际示例
    • 我将原始 json 编辑为实际的(删除个人信息)link
    猜你喜欢
    • 2017-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-09
    相关资源
    最近更新 更多