簡體   English   中英

如何從多個文件中提取特定的圖案行並獲取每行的文件名和修改日期-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文件中以“ Applying”開始行,並帶有其他一些必需的信息(全部在1行中,所有這些都是必需的)。
  • 如果不更新部分,則以“未應用”開頭。 我目前沒有需要這個信息。

我正在嘗試做的是:

我正在嘗試:-.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工作表中。

它不做的是:

在正確的行上放置正確的修改日期。 -此刻,它獲取第一個文件,並聲明所有行的文件修改日期相同。

謝謝! 抱歉,很長的帖子,我想確保在此提供了所有必需的詳細信息。 感謝您提供的任何幫助。 我對Powershell還是很陌生,目前仍在嘗試將它們拼湊起來,請耐心等待。

我將利用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

希望能有所幫助

定義$ lastModifiedDate然后將其設置為“ Updated Date”的值的方式將導致變量永不更改,而不管$ myString來自哪個日志文件。 我將分別查看每個文件,以便在創建表時每個文件的LastWriteTime不斷發展。 我會嘗試的:

$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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM