使用PowerShell过滤日志文件以创建csv报告

问题描述 投票:1回答:1

我在日志文件中有一个NetApp日志输出,格式如下。

DeviceDetails.log文件内容

  /vol/DBCXARCHIVE002_E_Q22014_journal/DBCXARCHIVE002_E_Q22014_journal    1.0t (1149038714880) (r/w, online, mapped)
    Comment: " "
    Serial#: e3eOF4y4SRrc
    Share: none
    Space Reservation: enabled (not honored by containing Aggregate)
    Multiprotocol Type: windows_2008
    Maps: DBCXARCHIVE003=33
    Occupied Size: 1004.0g (1077986099200)
    Creation Time: Wed Apr 30 20:14:51 IST 2014
    Cluster Shared Volume Information: 0x0 
    Read-Only: disabled
/vol/DBCXARCHIVE002_E_Q32014_journal/DBCXARCHIVE002_E_Q32014_journal  900.1g (966429273600)  (r/w, online, mapped)
    Comment: " "
    Serial#: e3eOF507DSuU
    Share: none
    Space Reservation: enabled (not honored by containing Aggregate)
    Multiprotocol Type: windows_2008
    Maps: DBCXARCHIVE003=34
    Occupied Size:  716.7g (769556951040) 
    Creation Time: Tue Aug 12 20:24:14 IST 2014
    Cluster Shared Volume Information: 0x0 
    Read-Only: disabled 

其中输出只有2个设备,它在日志文件中附加了多于x个设备。

我只需要每个模块的4个细节,第一行包含3个需要的细节

设备名称:/ vol / DBCXARCHIVE002_E_Q22014_journal / DBCXARCHIVE002_E_Q22014_journal

总容量:1.0t(1149038714880)

状态:(r / w,在线,映射)

我需要的第4个细节是占用尺寸:1004.0g(1077986099200)

所以CSV输出应如下所示:enter image description here

我不仅仅是编码的初学者,并试图通过以下代码实现这一点,但它没有多大帮助:/

$logfile = Get-Content .\DeviceDetails.log
$l1 = $logfile | select-string "/vol"
$l2 = $logfile | select-string "Occupied Size: " 

$objs =@()
$l1 | ForEach {
$o = $_ 
$l2 | ForEach {
    $o1 = $_
    $Object22 = New-Object PSObject -Property @{
        'LUN Name , Total Space, Status, Occupied Size'  = "$o"
        'Occupied Size'  = "$o1"           
    }           

}
$objs += $Object22  
}
$objs
powershell csv log-files
1个回答
0
投票
$obj = $null # variable to store each output object temporarily
Get-Content .\t.txt | ForEach-Object { # loop over input lines
  if ($_ -match '^\s*(/vol.+?)\s+(.+? \(.+?\))\s+(\(.+?\))') {
    # Create a custom object with all properties of interest,
    # and store it in the $obj variable created above.
    # What the regex's capture groups - (...) - captured is available in the
    # the automatic $Matches variable via indices starting at 1.
    $obj = [pscustomobject] @{
      'Device Name' = $Matches[1]
      'Total Space' = $Matches[2]
      'Status' = $Matches[3]
      'Occupied Size' = $null # filled below
    }
  } elseif ($_ -match '\bOccupied Size: (.*)') {
    # Set the 'Occupied Size' property value...
    $obj.'Occupied Size' = $Matches[1]
    # ... and output the complete object.
    $obj
  }
} | Export-Csv -NoTypeInformation out.csv

- 请注意,Export-Csv默认为ASCII输出编码;用-Encoding参数改变它。 - 要仅为(...)Total Space列提取Occupied Size中的数字,请使用 $_ -match '^\s*(/vol.+?)\s+.+?\s+\((.+?)\)\s+(\(.+?\))'$_ -match '\bOccupied Size: .+? \((.*)\)'代替。

请注意此解决方案如何逐行处理输入文件,这会使内存耗尽,但通常会牺牲性能。


至于你试过的:

  • 您将整个输入文件收集为内存中的数组($logfile = Get-Content .\DeviceDetails.log
  • 然后,您将此数组过滤两次到并行数组,其中包含相应的感兴趣的行。
  • 当您尝试嵌套这两个数组的处理时出现问题。您必须并行枚举它们,而不是嵌套,因为它们的相应索引包含匹配的条目。
  • 另外: 像'LUN Name , Total Space, Status, Occupied Size' = "$o"这样的一行创建了一个名为LUN Name , Total Space, Status, Occupied Size的属性,这不是意图。 为了创建不同的属性(在CSV输出中反映为不同的列),您必须创建它们,这需要相应地将输入解析为不同的值。
© www.soinside.com 2019 - 2024. All rights reserved.