Search code examples
powershellloggingexport-to-csv

Powershell: Create CSV list containing newest line in newest file in N number subdirs


I'm pretty green in terms of Powershell, so this presents a challenge to me:

Objective: Traverse N number of subdirs containing logs, find the newest file in each subdir, extract the last written line and add this to file.

This lists the directories containing logs - these dirs come and go beyond my control.

Get-ChildItem Z:\Logfiles | Where{$_.LastWriteTime -gt (Get-Date).AddDays(-1)}

and I have found this snippet to extract the last line from last written file in a named dir, where each dir contains logs from server named as folder name (mostly these are IP addresses):

gci Z:\Logfiles\172.16.1.1 | sort LastWriteTime | select -last 1 | Get-Content | Select-Object -Last 1

I need to merge these so that I can append these last lines to a file, perhaps as CSV - all hints are appreciated.


Solution

  • You could use something like this, I added comments so it's easier to understand the logic.

    # List all folders on the Initial Path
    $folders = Get-ChildItem -Directory -Path Z:\Logfiles
    $export = [system.collections.generic.list[pscustomobject]]::new()
    
    foreach($folder in $folders)
    {
        # Get the newest file in each folder
        $file = Get-ChildItem $folder -File | Sort-Object LastWriteTime -Descending | Select -First 1
    
        # Read the content of the file and get the last line
        $content = (Get-Content $file)[-1]
    
        # Here you can create a new object that will be used to export to Csv
        # As an example, i'll create an object with the File Name,
        # Last Write Time and Last Line of the Content (This will be the CSV columns)
        $export.Add(
            [PSCustomObject]@{
                FileName = $file.Name
                LastWriteTime = $file.LastWriteTime
                LastLine = $content
        })
    }
    
    # After the loop finishes you can export the results to a Csv
    $export | Export-Csv -NoTypeInformation -Path "Path/To/Export/Here.csv"