Apache Log Parser - Snowboundport37/champlain GitHub Wiki
Apache Log Parser – Deliverable 1
This project parses Apache access logs generated by XAMPP.
The PowerShell function reads the log word by word into a PSCustomObject
and filters the results to show only addresses in the 10. network*.
How to Run
Open PowerShell (or PowerShell ISE) in this repository folder and run:
powershell -ExecutionPolicy Bypass -File .\main.ps1
The script will output a table of log entries where the IP address starts with 10..
Parser Function (Apache-Parse.ps1)
function Get-ApacheTable10 {
param([string]$Path = "C:\xampp\apache\logs\access.log")
if (-not (Test-Path $Path)) { throw "Log not found at $Path" }
$lines = Get-Content -Path $Path
$records = @()
for ($i = 0; $i -lt $lines.Count; $i++) {
$line = $lines[$i]
if ([string]::IsNullOrWhiteSpace($line)) { continue }
# Split the log line into individual words
$w = $line.Split(' ')
if ($w.Count -lt 9) { continue }
# Build a custom object for each log line
$records += [pscustomobject]@{
IP = $w[0]
Time = $w[3].Trim('[')
Method = $w[5].Trim('"')
Page = $w[6]
Protocol = $w[7].Trim('"')
Response = $w[8]
Referrer = if ($w.Count -ge 11) { $w[10].Trim('"') } else { "-" }
Client = if ($w.Count -ge 12) { ($w[11..($w.Count-1)] -join ' ').Trim('"') } else { "-" }
}
}
# Return only addresses from the 10.* network
return $records | Where-Object { $_.IP -like '10.*' }
}
Main Script (main.ps1)
# Load the parser function
. "$PSScriptRoot\Apache-Parse.ps1"
# Call the function and format the output as a table
$tableRecords = Get-ApacheTable10 -Path "C:\xampp\apache\logs\access.log"
$tableRecords |
Format-Table IP, Time, Method, Page, Protocol, Response, Referrer -AutoSize -Wrap
Screenshot
Insert a screenshot of the script running successfully below
(showing the formatted table of 10.* IP addresses).
Notes
- Make sure Apache is running in XAMPP and that you have generated traffic (visit existing and non-existing pages) so the access log contains entries.
$PSScriptRootensures the function file is loaded from the same folder asmain.ps1.- Both scripts should be committed to this repository along with this README and the screenshot.