Powershell解析xml日志文件&获取当前解析的文件名

时间:2016-01-15 07:08:14

标签: regex xml powershell gridview

我是PowerShell的新手,需要指导。一直在搜索网站寻找答案并且空白,决定问一下。如果已经回答,请参阅链接。

我有一个应用程序日志(xml格式),如下所示:

<log><identifier>123axr4x5</identifier><login>USER1</login><source>Order-Management</source><AddlInfo>Execution Time : 20ms</AddlInfo><Exception></Exception><timestamp>01/01/2015:22:00:00</timestamp><serverticks>643670855</serverticks><PID>1234</PID><Machine>PRD01X12mm</Machine></log>

<log><identifier>dd8jksl3g</identifier><login>USER2</login><source>Service-Assurance</source><AddlInfo>Execution Time : 80ms</AddlInfo><Exception></Exception><timestamp>01/01/2015:22:00:00</timestamp><serverticks>643680865</serverticks><PID>1234</PID><Machine>PRD01X12mm</Machine></log>
 : and so on

我正在创建一个日志解析器,它将扫描文件夹及其子文件夹以匹配正则表达式模式,并根据特定阈值输出到gridview / export到CSV。我差不多完成了,但是我无法解决1个问题,是获取当前正在解析的文件名,以便在gridview上显示。

基本上我使用管道式Get-ChildItem,如下所示

Get-ChildItem $Dir -recurse -Filter *logging*.txt|
Sort-Object LastWriteTime | 
?{$_.LastWriteTime -gt (Get-Date).AddMinutes(-60)}|
Select-String -Pattern $Text  |
Select-String -Pattern $Text3  |
Select-String -Pattern $Text2 -allmatches |
Foreach-Object { 
$information = $_|Select-Object -Property API, Duration,DataRetrieved, ServerTime, ServerTicks , Identifier, Filename 
$information.Filename = $_.Name    
#$information.Filename = $_.FullName 

} |
Out-GridView 

以下是完整代码:

$Dir = "C:\log\"
$threshold = 1 + 0

$StartTime = (Get-Date).ToString();
$EndTime = (Get-Date).ToString();

$Text = "abc"
$Text2 = "def"
$Text3 = "ghi"
$OutFile = "result"

$OutPath = $Dir + $OutFile + ".txt"

#ExtractionParameters
$AddlInnfoTagBegin = "AddlInfo"
$AddlInnfoTagEnd = "/AddlInfo"
$ServerTimeOfLogTagBegin = "ServerTimeOfLog"
$ServerTimeOfLogTagEnd = "/ServerTimeOfLog"
$ServerTicksTagBegin = "ServerTicks"
$ServerTicksTagEnd = "/ServerTicks"
$IdentifierTagBegin = "Identifier"
$IdentifierTagEnd = "/Identifier"

#parse file in folders
Get-ChildItem $Dir -recurse -Filter *logging*.txt|
Sort-Object LastWriteTime | 
#?{$_.LastWriteTime -gt (Get-Date).AddMinutes(-60)}|
Select-String -Pattern $Text  |
Select-String -Pattern $Text3  |
Select-String -Pattern $Text2 -allmatches |
 Foreach-Object {

    # take line and split it at tabulators
    $parts = $_.Line

    #write $parts
    $indexOfAddlInfoBegin = $parts.IndexOf($AddlInnfoTagBegin) + $AddlInnfoTagBegin.Length +1
    $indexOfAddlInfoEnd = $parts.IndexOf($AddlInnfoTagEnd) -1

    $AddlInfoData = $parts.Substring($indexOfAddlInfoBegin, $indexOfAddlInfoEnd - $indexOfAddlInfoBegin)
    $AddlInfoReplaced = $AddlInfoData.Replace(" seconds ","@")
    $AddlInfoSplit = $AddlInfoReplaced.Split('@')
    $information = $_|Select-Object -Property API, Duration,DataRetrieved, ServerTime, ServerTicks , Identifier, Filename   

    #get filename, which does not work
    $information.Filename = $_.Name 
    #$information.Filename = $_.FullName 

    $information.API =  $AddlInfoSplit[0].Split(':')[0]

    $information.DataRetrieved =  $AddlInfoSplit[1]
    $information.Duration = $AddlInfoSplit[0].Split(':')[1]
    $information.Duration = $information.Duration.Replace("Execution Time = ","")

    $indexOfServerTimeBegin = $parts.IndexOf($ServerTimeOfLogTagBegin) + $ServerTimeOfLogTagBegin.Length +1
    $indexOfServerTimeEnd = $parts.IndexOf($ServerTimeOfLogTagEnd) -1
    $ServerTimeData = $parts.Substring($indexOfServerTimeBegin, $indexOfServerTimeEnd - $indexOfServerTimeBegin)
    $information.ServerTime = $ServerTimeData


    $indexOfServerTicksBegin = $parts.IndexOf($ServerTicksTagBegin) + $ServerTicksTagBegin.Length +1
    $indexOfServerTicksEnd = $parts.IndexOf($ServerTicksTagEnd) -1
    $ServerTickData = $parts.Substring($indexOfServerTicksBegin, $indexOfServerTicksEnd - $indexOfServerTicksBegin)
    $information.ServerTicks = $ServerTickData 

    $indexOfIdentifierBegin = $parts.IndexOf($IdentifierTagBegin) + $IdentifierTagBegin.Length +1
    $indexOfIdentifierEnd = $parts.IndexOf($IdentifierTagEnd) -1

    $IdentifierData = $parts.Substring($indexOfIdentifierBegin, $indexOfIdentifierEnd - $indexOfIdentifierBegin)
    $information.Identifier = $IdentifierData 

    $DurationAsInt = 0 + $information.Duration 
    if($DurationAsInt -gt $threshold) {
    write $information
    }
 } |
 Out-GridView
#Out-File -FilePath $OutPath -Append -Width 200 

感谢任何帮助,谢谢!!

-CL

1 个答案:

答案 0 :(得分:1)

您要查找的属性是&#34; FileName&#34;。

  $information.Filename = $_.FileName 

Powershell提供了一个cmdlet&#34; Get-Member&#34;这将列出所有可用的属性/方法。您可以枚举成员以控制并检查可用的内容

  Write-Host ( $_ | Get-Member)