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

Regex Powershell解析xml日志文件&;获取当前解析的文件名,regex,xml,powershell,gridview,Regex,Xml,Powershell,Gridview,我是powershell的新手,需要指导。一直在网站上搜寻答案,结果一无所获,于是决定改问。如果已经回答了这个问题,请让我参考链接 我有一个应用程序日志(xml格式),如下所示: <log><identifier>123axr4x5</identifier><login>USER1</login><source>Order-Management</source><AddlInfo>Execution

我是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
以下是完整代码:

$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

您要查找的属性是“FileName”

Powershell提供了一个cmdlet“Get Member”,它将列出所有可用的属性/方法。您可以枚举成员来控制台和检查可用的内容

  Write-Host ( $_ | Get-Member)

谢谢你,巴斯基!很高兴了解Get-Member cmdlet,再次感谢!
  $information.Filename = $_.FileName 
  Write-Host ( $_ | Get-Member)