Last active
October 31, 2025 19:21
-
-
Save Tomamais/d7d18f6a1a470eb85b1eb10eb27c1685 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <# | |
| .SYNOPSIS | |
| Reads, parses, and displays a tab-separated log file in a filterable grid view, | |
| ordered by the most recent log entries. | |
| #> | |
| param( | |
| [Parameter(Mandatory=$true)] | |
| [string]$LogFilePath | |
| ) | |
| # --- Function to Read and Structure the Tab-Separated Log --- | |
| function Get-TabDelimitedLogs { | |
| param( | |
| [string]$Path | |
| ) | |
| Write-Host "Reading and converting log file..." -ForegroundColor Cyan | |
| try { | |
| # 1. Read the content. | |
| # 2. Use ConvertFrom-Csv: | |
| # -Header defines the columns based on your format (2025-10-31 3:00:00 PM HostName AppName LogLevel UserName Category Message). | |
| # -Delimiter "`t" specifies the tab character for separation. | |
| # -Skip 1 is used to skip the first line if it contains the column headers, as the header is defined above. | |
| $Logs = Get-Content -Path $Path | | |
| ConvertFrom-Csv -Delimiter "`t" -Header "Timestamp", "HostName", "AppName", "LogLevel", "UserName", "Category", "Message" | |
| # 3. Process and convert the Timestamp column to a proper DateTime object | |
| $Logs = $Logs | Select-Object *, @{Name='DateTime'; Expression={[datetime]$_.Timestamp}} -ExcludeProperty Timestamp | |
| # 4. Sort the objects by the new DateTime column (most recent first) | |
| return $Logs | Sort-Object -Property DateTime -Descending | |
| } catch { | |
| Write-Error "Error parsing log file: $($_.Exception.Message)" | |
| return @() # Return an empty array on error | |
| } | |
| } | |
| # --- End of Function --- | |
| function Show-LogGridView { | |
| param( | |
| [string]$Path | |
| ) | |
| Write-Host "Loading logs from: $Path..." -ForegroundColor Green | |
| # 1. Parse and sort the logs | |
| $Logs = Get-TabDelimitedLogs -Path $Path | |
| if (-not $Logs) { | |
| Write-Host "No log entries found or file parsing failed. Cannot show grid." -ForegroundColor Red | |
| return | |
| } | |
| # 2. Display in Out-GridView | |
| Write-Host "Displaying grid. Close the window to refresh/exit." -ForegroundColor Yellow | |
| # Out-GridView provides the filter and sort capabilities automatically | |
| $Logs | Out-GridView -Title "Log Viewer - $Path (Close to Refresh/Exit)" -Wait | |
| } | |
| # --- Main Loop for Refresh Option --- | |
| while ($true) { | |
| Show-LogGridView -Path $LogFilePath | |
| # After the user closes Out-GridView, ask if they want to refresh | |
| $Choice = Read-Host "`nLogs displayed. Do you want to [R]efresh or [E]xit? (R/E)" | |
| if ($Choice -ne "r" -and $Choice -ne "R") { | |
| break | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment