PowerShell Log File Text Finder Toolkit

# PowerShell Log File Text Finder Toolkit

# Function to search for text in log files
function Search-LogFile {
    param (
        [string]$LogFilePath,
        [string]$SearchText,
        [string]$OutputFilePath
    )
    
    if (-Not (Test-Path $LogFilePath)) {
        Write-Host "The specified log file does not exist."
        return
    }
    
    # Search the log file for the specified text
    Write-Host "Searching for '$SearchText' in $LogFilePath..."
    $results = Select-String -Path $LogFilePath -Pattern $SearchText
    
    if ($results.Count -eq 0) {
        Write-Host "No matches found."
    } else {
        # Display results
        $results | Format-Table LineNumber, Line -AutoSize
        Write-Host "Found $($results.Count) matches."
        
        # Optionally save results to output file
        if ($OutputFilePath) {
            $results | Out-File -FilePath $OutputFilePath -Append
            Write-Host "Results saved to $OutputFilePath."
        }
    }
}

# Function to display the menu and handle user choices
function Show-Menu {
    Clear-Host
    Write-Host "Log File Text Finder Toolkit"
    Write-Host "1: Search in Log File"
    Write-Host "Q: Quit"
    $choice = Read-Host "Enter your choice"
    
    switch ($choice) {
        "1" {
            $logFilePath = Read-Host "Enter log file path"
            $searchText = Read-Host "Enter text to search"
            $outputFilePath = Read-Host "Enter output file path (or press Enter to skip saving)"
            
            if ([string]::IsNullOrEmpty($outputFilePath)) {
                $outputFilePath = $null
            }
            
            Search-LogFile -LogFilePath $logFilePath -SearchText $searchText -OutputFilePath $outputFilePath
        }
        "Q" { exit }
        default { Write-Host "Invalid choice, please try again." }
    }
}

# Main loop to display the menu and process user choices
while ($true) {
    Show-Menu
}

Explanation:

  1. Function: Search-LogFile
    • Parameters:
      • $LogFilePath: The path to the log file where the search will be performed.
      • $SearchText: The text pattern to search for in the log file.
      • $OutputFilePath: Optional path to save the search results.
    • Logic:
      • Checks if the log file exists.
      • Uses Select-String to search for the specified text pattern.
      • Displays the matching lines and line numbers.
      • Optionally saves the results to an output file if specified.
  2. Function: Show-Menu
    • Displays a menu to the user for searching within log files or quitting the script.
    • Handles user input to perform the search or exit the script.
  3. Main Loop
    • Continuously displays the menu and processes user input.

Usage:

  1. Save the Script: Copy the script into a .ps1 file, e.g., LogFileTextFinder.ps1.
  2. Run the Script: Open PowerShell and execute the script by navigating to its directory and running:.\LogFileTextFinder.ps1
  3. Follow Prompts: Use the menu to enter the log file path, search text, and optionally specify an output file path.

This single-file PowerShell toolkit is a simple yet powerful tool for searching text within log files, displaying results, and optionally saving them. It can be easily extended or modified to fit additional requirements or handle more complex scenarios.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *