The issue is that in my Excel output it does not stop after the subject line has been proceeded. I have a utm9 smtp proxy log and try to output date sender receiver and subject only. So it should stop as soon as the subject ends.
Here is some example of the log file:
2025:01:15-16:22:31 sutm1kor-2 exim-in[11594]: [104\126] F From: xxxS harma <[email protected]>
2025:01:15-16:22:31 sutm1kor-2 exim-in[11594]: [105\126] T To: "[email protected]" <[email protected]>,
2025:01:15-16:22:31 sutm1kor-2 exim-in[11594]: [106\126] "[email protected]" <[email protected]>
2025:01:15-16:22:31 sutm1kor-2 exim-in[11594]: [107\126] C CC: "xx, xsss [ADD IT]" <[email protected]>, "xxx, xx[ADD
2025:01:15-16:22:31 sutm1kor-2 exim-in[11594]: [108\126] IT]" <[email protected]>
2025:01:15-16:22:31 sutm1kor-2 exim-in[11594]: [109\126] Subject: xxArcserve-Wartungsvertrahhhg N. xxx& Co. GmbH hh wird
2025:01:15-16:22:31 sutm1kor-2 exim-in[11594]: [110\126] am 1.3.2025 ablaufen
2025:01:15-16:22:31 sutm1kor-2 exim-in[11594]: [111\126] Thread-Topic: Ihr xx-Wartungsvertrag xx. xx& xx. GmbH xx
2025:01:15-16:22:31 sutm1kor-2 exim-in[11594]: [112\126] wird am 1.3.2025 ablaufen
2025:01:15-16:22:31 sutm1kor-2 exim-in[11594]: [113\126] Thread-Index: AQHbZEVvoZQu1Fou30O6c9m84wfi5rMX+aUw
2025:01:15-16:22:31 sutm1kor-2 exim-in[11594]: [114\126] Date: Wed, 15 Jan 2025 15:21:07 +0000
2025:01:15-16:22:31 sutm1kor-2 exim-in[11594]: [115\126] I Message-ID: <PH0PR10MB4711CA54F8FD03D520F60105E9192@PH0PR10MB4711.namprd10.prod.outlook.com>
So what my script does in the subject line it copy everything after Thread-Index, but it should not do that but go to the next line that starts by F From: and process the next and so on.
The Excel app says as well the file is broken and need repair, but it still seems to have all I need. How can I fix my code?
# Define paths
$logFilePath = "C:\1.log"
$outputExcelPath = "C:\path\to\your\output.xlsx"
# Ensure ImportExcel module is installed
if (-not (Get-Module -ListAvailable -Name ImportExcel)) {
Write-Host "Installing ImportExcel module..."
Install-Module ImportExcel -Force -Scope CurrentUser
}
# Initialize arrays and variables
$emailData = @()
$mailaddress_regex = "([a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})"
$addtonextline = $false
$lastline = ""
$date = $null
$senders = @()
$receivers = @()
$ccs = @()
$subject = ""
# Read the log file into an array (reverse order for multi-line handling)
$text = Get-Content -Path $logFilePath
for ($i = $text.Count - 1; $i -ge 0; $i--) {
$line = $text[$i]
# Append last line if necessary (handling multi-line fields)
if ($addtonextline) {
$line = $line + " " + $lastline
}
# Capture date/time from log entry
if ($line -match '(\d{4}:\d{2}:\d{2}-\d{2}:\d{2}:\d{2})') {
if ($date -and $senders -and $receivers -and $subject) {
# Store the parsed email data
$emailData += [PSCustomObject]@{
Date = $date
Sender = ($senders -join ", ")
Receiver = ($receivers -join ", ")
CC = ($ccs -join ", ")
Subject = $subject
}
# Reset values for next email entry
$senders = @()
$receivers = @()
$ccs = @()
$subject = ""
}
$date = $matches[1]
}
# Extract sender email using Select-String
if ($line -match "F From") {
$senders += Select-String -Pattern $mailaddress_regex -InputObject $line -AllMatches | ForEach-Object { $_.Matches.Value }
$addtonextline = $false
}
# Extract recipient email(s) using Select-String
elseif ($line -match "T To") {
$receivers += Select-String -Pattern $mailaddress_regex -InputObject $line -AllMatches | ForEach-Object { $_.Matches.Value }
$addtonextline = $false
}
# Extract CC email(s) using Select-String
elseif ($line -match "C CC") {
$ccs += Select-String -Pattern $mailaddress_regex -InputObject $line -AllMatches | ForEach-Object { $_.Matches.Value }
$addtonextline = $false
}
# Extract subject line (may be multi-line)
elseif ($line -match "Subject: (.+)") {
$subject = $matches[1]
$addtonextline = $false
}
# Handle multi-line continuation
else {
if ($line -match "\[\d+\\\d+\]\s(.*)") {
$lastline = $matches[1]
$addtonextline = $true
}
}
}
# Add the last email entry if any data exists
if ($date -and $senders -and $receivers -and $subject) {
$emailData += [PSCustomObject]@{
Date = $date
Sender = ($senders -join ", ")
Receiver = ($receivers -join ", ")
CC = ($ccs -join ", ")
Subject = $subject
}
}
# Export the data to an Excel file
$emailData | Export-Excel -Path $outputExcelPath -AutoSize
Write-Host "Data has been successfully exported to $outputExcelPath"
Message-IDline.