如何从多个文件中提取特定的模式行并获取每行的文件名和修改日期 - Powershell

How to extract specific pattern lines from multiple files AND get the file name & date modified for each line - Powershell

过程总结:

有一个创建日志文件的修补过程,这些文件都进入 1 个文件夹并且不会再次更新或修改。所有补丁的所有日志文件都放在这里,不会被覆盖。

本次补丁中,对多个区域进行了多次更新,但只会更新版本低于新更新的部分。 (例如 'Section.X' 的版本是 12,补丁是 Section.X 15 <- 这将更新。如果补丁是 Section.X 11 它不会更新。)

我要做什么:

我正在尝试: - .log 文件中的特定行。 - 他们来自的文件名。 - 他们来自的文件的修改日期。

到目前为止的测试/脚本:

#Creating the String, and applying the filter to only take the 'Applying' lines, and then creating a new line for it to repeat.
$myString = (Select-String -Path "C:\FilePathHere\*.log" -Pattern '^Applying ').Line

# This will take the Modified date from the File.
$lastModifiedDate = (Get-Item "C:\FilePathHere\*.log").LastWriteTime

$arr = $myString -split ' '

$Groups1 = @()
$Groups = @()

#Create a table holding just the specific info from each line required, and putting that into a table.

foreach($members in $myString)
        {
            $arr = $members -split ' '

            $Groups1 = New-Object PSObject 
            $Groups1 | Add-Member -type NoteProperty -Name 'Object Updated' -Value $arr[1]
            $Groups1 | Add-Member -type NoteProperty -Name 'New Version' -Value $arr[3]
            $Groups1 | Add-Member -type NoteProperty -Name 'Old Version' -Value $arr[8]
            $Groups1 | Add-Member -type NoteProperty -Name 'Server/DB' -Value $arr[5]
            $Groups1 | Add-Member -type NoteProperty -Name 'Updated Date' -Value $lastModifiedDate
            $Groups += $Groups1  
        }  
# to display the recorded and ordered info:
$Groups

目前遇到的问题:

所以,以上内容实际上完成了我想要的大部分内容。它做什么: - 它采用正确的线路。 - 它正确地订购了它。 - 它只需要所需的信息。 - 它正确地将其输入 Excel sheet.

它不做的是:

在正确的行中输入正确的修改日期。 - 目前,它获取第一个文件并声明所有行都具有相同的文件修改日期。

谢谢! 很抱歉 post,我想确保我提供了此处所需的所有详细信息。感谢您提供的任何帮助。我是 Powershell 的新手,目前仍在努力拼凑,请耐心等待。

您定义 $lastModifiedDate 然后将其设置为 'Updated Date' 的值的方式导致变量永远不会更改,无论 $myString 来自哪个日志文件。我会单独查看每个文件,以便每个文件的 LastWriteTime 在创建我的 table 时不断变化。我会试试这个:

$logfiles = Get-ChildItem -Path C:\FilePathHere\*.log

$Groups1 = @()
$Groups = @()

foreach ($logfile in $logfiles) {

    $myString = (Select-String -Path "C:\FilePathHere$($logfile.Name)" -Pattern '^Applying ').Line

    $arr = $myString -split ' '

    $lastModifiedDate = $logfile.LastWriteTime

    foreach ($members in $myString) {

        $arr = $members -split ' '

        $Groups1 = New-Object PSObject 
        $Groups1 | Add-Member -type NoteProperty -Name 'Object Updated' -Value $arr[1]
        $Groups1 | Add-Member -type NoteProperty -Name 'New Version' -Value $arr[3]
        $Groups1 | Add-Member -type NoteProperty -Name 'Old Version' -Value $arr[8]
        $Groups1 | Add-Member -type NoteProperty -Name 'Server/DB' -Value $arr[5]
        $Groups1 | Add-Member -type NoteProperty -Name 'Updated Date' -Value $lastModifiedDate
        $Groups += $Groups1  

    }

}

# to display the recorded and ordered info:
$Groups

我会利用 Select-String cmdlet 上的 -Path 参数来缩短循环并将 PsObjects 数组输出到变量:

$result = Select-String -Path 'C:\FilePathHere\*.log' -Pattern '^Applying ' | ForEach-Object {
    $arr = $_.Line -split ' '
    [PsCustomObject]@{
        'Object Updated' = $arr[1]
        'New Version'    = $arr[3]
        'Old Version'    = $arr[8]
        'Server/DB'      = $arr[5]
        'LogFile'        = $_.Path
        'Updated Date'   = (Get-Item -Path $_.Path).LastWriteTime
    }
}

# output on screen
$result

# output to CSV
$result | Export-Csv -Path "D:\logInfo.csv" -NoTypeInformation

希望对您有所帮助