← script library

Enhancing System Monitoring with PowerShell: Real-time Log Management

Monitoring & HealthDecember 29, 2024

In today's fast-paced digital landscape, maintaining an efficient and secure IT environment is crucial for organizational success. Effective monitoring not only helps in identifying issues early but also aids in maintaining system performance. PowerShell provides a robust framework that enables system administrators to automate various tasks related to log management. Log management involves collecting, analyzing, and monitoring log data generated by systems, applications, and devices. Proper log management allows administrators to detect security incidents, troubleshoot issues, and enhance compliance with regulatory standards. Automating this process can streamline operations and reduce the administrative burden. This script continuously monitors specific log files, filters relevant entries, and displays them in real-time. This is particularly useful for IT departments looking to oversee events such as system errors, security alerts, or application failures.

powershell
$logPath = "C:\Logs\YourLogFile.log"

function Monitor-Log {
    param (
        [string]$Path
    )
    Get-Content -Path $Path -Wait | ForEach-Object {
        $logEntry = $_
        # Filter specific log entries, e.g., errors
        if ($logEntry -match "ERROR") {
            Write-Host "Error detected: $logEntry" -ForegroundColor Red
        } elseif ($logEntry -match "WARNING") {
            Write-Host "Warning detected: $logEntry" -ForegroundColor Yellow
        }
    }
}

Monitor-Log -Path $logPath

function Send-Alert {
    param (
        [string]$Message
    )
    Send-MailMessage -To "[email protected]" -From "[email protected]" `
                     -Subject "Log Alert" -Body $Message -SmtpServer "smtp.example.com"
}
# Call Send-Alert within the monitor function when errors are detected
if ($logEntry -match "ERROR") {
    Send-Alert -Message $logEntry
}

Run it across your fleet

This script runs as-is on a single host. Paste it into ServerEngine to schedule it, run it on a whole server group in parallel, and keep the credentials out of the file — see the scripts documentation and the credential store.

Ready when you are.

Try ServerEngine free for 7 days.