【问题标题】:Adding a Column to CSV Using Data Returned From Function使用从函数返回的数据将列添加到 CSV
【发布时间】:2018-07-04 00:37:09
【问题描述】:

我一直在编写一个脚本,该脚本从包含防火墙日志的 syslog 服务器获取输出,以更易读的格式将该输出重新组织为新的 CSV 文件,然后查找源 IP 地址以识别城市交通从何而来。 我不确定如何从我的函数中获取结果,该函数会查找 IP 地址的详细信息以向新的 CSV 文件添加其他列。

这是来自源文件的单个记录的示例。注意:我已经编辑了 IP 地址以屏蔽它们。

2018-07-01 14:48:47,Local7.Info,192.168.1.00,device="SFW" 日期=2018-07-01 时间=14:48:39 timezone="PDT" device_name="XG" device_id=00000000000000 log_id=010101600001 log_type="防火墙" log_component="防火墙规则" log_subtype="允许" status="允许" 优先级=信息持续时间=11 fw_rule_id=3 policy_type=3 user_name=""user_gp=""iap=0 ips_policy_id=0 appfilter_policy_id=0 application="安全套接层协议" application_risk=1 application_technology="网络协议" application_category="基础设施" in_interface="Port2" out_interface="Port1" src_mac=00: 0:00: 0:00: 0 src_ip=75.148.000.000 src_country_code=美国 dst_ip=23.24.000.000 dst_country_code=美国 协议="TCP" src_port=55000 dst_port=443 sent_pkts=7 recv_pkts=6 sent_bytes=1369 recv_bytes=918 tran_src_ip=192.168.000.000 tran_src_port=0 tran_dst_ip=192.168.000.000 tran_dst_port=0 srczonetype="WAN" srczone="WAN" dstzonetype="LOCAL" dstzone="LOCAL" dir_disp="" connevent="停止" connid="1782869248" vconnid="" hb_health="无心跳" message="" appresolvedby="签名"

在此处其他用户的帮助下,我已经能够编写一个脚本来创建一个新的 CSV 文件 ($output_file),该文件具有更易于阅读的格式,并查找信息在 src_ip 列上。我不确定如何从该函数 (Get-IPGeolocation) 中获取结果,并将其他列添加到我的 $output_file 中。

#Parameters for environment
$regex = '\b(\w+)=([^ ]+)'
$input_path = "C:\powershell_work\ParsingSophos\data.csv"
$output_file = "C:\powershell_work\GeolocatingIPs\combine-output.csv"


#Function used to lookup IP-Address information.
function Get-IPGeolocation
{
    Param
    (
        [string]$IPAddress
    )

    $request = Invoke-RestMethod -Method Get -Uri "http://geoip.nekudo.com/api/$IPAddress"

    [PSCustomObject]@{
        IP        = $request.IP
        City      = $request.City
        Country   = $request.Country.Name
        Code      = $request.Country.Code
        Location  = $request.Location.Latitude
        Longitude = $request.Location.Longitude
        TimeZone  = $request.Location.Time_zone
    }
}

# Parsing the input_path file, and exporting to a more workable format
Select-String -LiteralPath $input_path -AllMatches -Pattern $regex | ForEach-Object {
    $obj = New-Object pscustomobject
    foreach ($match in $_.Matches) {
      Add-Member -InputObject $obj -NotePropertyName $match.Groups[1].Value -NotePropertyValue $match.Groups[2].Value
    }
    $obj
} | Select-Object *_ip, srczone, src_country_code, dstzone, dst_country_code | 
     Export-Csv -NoTypeInformation $output_file


# Gathering list of Source IPs to be used in the lookup
$src_ips = Import-CSV $output_file | select -ExpandProperty src_ip


# Loop that runs each IP through the function to find IP address information
ForEach($ip in $src_ips) {
Get-IPGeolocation -IPAddress $ip
}

使用 $input_path CSV 文件中的多条数据记录运行该脚本会返回如下结果。 Output Exmaple

【问题讨论】:

  • IMO 没有错。如果列数/宽度超出限制,PowerShell 会将输出切换到格式列表视图。尝试管道到| Format-Table -Auto
  • Get-Help about_Preference_Variables 查找$FormatEnumerationLimit default=4
  • @LotPings 这就是我上次被问到这个问题时告诉他们的。另外我不认为$FormatEnumerationLimit 在这里有帮助,因为这应该用于列中的数组没有?
  • 如何获取从函数“Get-IPGeolocation”返回的数据,以便将新列创建回 CSV 文件 $output_file?

标签: function powershell csv formatting


【解决方案1】:

本质上,您是在问如何合并两个自定义对象

一个简单的例子(PSv4+ 语法,假设属性名称集不重叠):

# Create two sample custom objects to merge.
$co1 = [pscustomobject] @{ one = 1; two = 2; three = 3 }
$co2 = [pscustomobject] @{ four = 4; five = 5; six = 6 }

# Add $co2's properties to $co1
$co2.psobject.properties.foreach({
  Add-Member -InputObject $co1 -NotePropertyName $_.Name -NotePropertyValue $_.Value
})

# Output $co1, which now contains its own properties plus the ones from $co2.
$co1

以上产出:

one   : 1
two   : 2
three : 3
four  : 4
five  : 5
six   : 6

应用于您的场景:

Import-Csv $output_file | ForEach-Object {
  $obj = $_
  (Get-IPGeolocation -IPAddress $obj.src_ip).psobject.properties.foreach({
    Add-Member -InputObject $obj -NotePropertyName $_.Name -NotePropertyValue $_.Value
  })
  $obj
} | Export-Csv -NoTypeInformation .\merged.csv

【讨论】:

  • @FrameWorkTeam:我的荣幸;很高兴听到它有帮助。
猜你喜欢
  • 2022-12-21
  • 1970-01-01
  • 2020-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-27
  • 1970-01-01
相关资源
最近更新 更多