# Initialize Excel sheet $path = Read-Host -Prompt "Enter path to save file (e.g. C:\path\to\file)" $IIS_PATH = "C:\Program Files\www\iisnode" # Create new Excel object $excel = New-Object -ComObject Excel.application $excel.SheetsInNewWorkbook = 1 # Create new Workbook $workbook = $excel.Workbooks.Add() # Create worksheet in workbook and make it active $worksheet = $workbook.Sheets.Item(1) $worksheet.Activate() # Loop through files in directory Get-ChildItem $IIS_NODE_PATH -Filter *.txt |Sort-Object LastWriteTime -Descending | ForEach-Object { $listOfFiles = New-Object System.Collections.ArrayList # Filter out error logs if ($_.FullName.Contains("std")){ # Add list of files [void] $listOfFiles.Add($_.FullName) # Regex for parsing data $r = [regex] "\[([^\[]*)\]" $dateIndex = 0 $verbosityIndex = 2 $moduleIndex = 3 $descriptionIndex = 4 # Create worksheet column headers $worksheet.Cells.Item(1,1) = "Date" $worksheet.Cells.Item(1,2) = "Verbosity" $worksheet.Cells.Item(1,3) = "Module" $worksheet.Cells.Item(1,4) = "Description" $row = 2 # Iterate over each file and read its contents foreach ($file in $listOfFiles){ Write-Output "Reading file: $file" Get-Content $file | Where-Object {$_ -match $r} | ForEach-Object { $column = 1 $line = $_ $match = $r.matches($line) try{ $date = [DateTime]$match[$dateIndex].groups[1].value $verbosity = $match[$verbosityIndex].groups[1].value $module = $match[$moduleIndex].groups[1].value $description = $match[$descriptionIndex].groups[1].value $worksheet.Cells.Item($row,$column) = $date $column++ $worksheet.Cells.Item($row,$column) = $verbosity $column++ $worksheet.Cells.Item($row,$column) = $module $column++ $worksheet.Cells.Item($row,$column) = $description $row++ } catch{ } } } } } # Check if entered path exists. Create if not if (!(Test-Path $path)){ Write-Host "$path doesn't exist. Creating..." New-Item -ItemType Directory -Force -Path $path } # Save Excel file $workbook.SaveAs($path + "\IISNodeLogs.xlsx") # Exit and activate GC $excel.Quit() $excel = $Null [gc]::Collect() [gc]::WaitForPendingFinalizers()