79402964

Date: 2025-01-31 14:04:56
Score: 2
Natty:
Report link
    I now use your version of the script 
    
        # 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"
    
    
    It works fine however it imports as well stuff in the subject line that should not be there. 



(Thread-Topic: Ihr Arcserve-Wartungsvertrag  & Co. GmbH 655 wird am 1.3.2025 ablaufen   Thread-Index: AQHbZEVvoZQu1Fou30O6c9m84wfi5rMX+aUw   Date: Wed, 15 Jan 2025 15:21:07 +0000 I Message-ID: <PH0PR10MB4711CA54F8FD03D520F60105E9192@PH0PR10MB4711.namprd10.prod.outlook.com>   References: <PH0PR10MB4711C96CBD32962D1ABD139BE91D2@PH0PR10MB4711.namprd10.prod.outlook.com>  <PH0PR10MB47114C6D235CB12AA2A3D0D0E91D2@PH0PR10MB4711.namprd10.prod.outlook.com>  <PH0PR10MB4711E2A726EAAC6CA37E1AABE91D2@PH0PR10MB4711.namprd10.prod.outlook.com>  <2488c69a-32f2-4a9a-9956-2b8e30793e84.dd301281-d445-472c-a307-12b501968a31.c1cfee43-cfd0-49ba-84b0-4e461c57bc8d@emailsignatures365.codetwo.com>   In-Reply-To: <PH0PR10MB4711E2A726EAAC6CA37E1AABE91D2@PH0PR10MB4711.namprd10.prod.outlook.com>   Accept-Language: en-US   X-MS-Has-Attach: yes   X-MS-TNEF-Correlator:    Authentication-Results-Original: dkim=none (message not signed)  header.d=none;dmarc=none action=none header.from 2025-01-15 16:22:40 1tY5E7-0000000032H-1sTw H=a27-154.smtp-out.us-west-2.amazonses.com [54.240.27.154]:52483 X=TLS1.2:ECDHE-RSA-AES128-SHA256:128 CV=no F=<010101946a8eb7f3-20e93eb5-9f40-441b-94eb-7a7057f0c2e2-000000@us-west-2.amazonses.com> temporarily rejected after DATA: Temporary local problem, please try again! Envelope-from: <010101946a8eb7f3-20e93eb5-9f40-441b-94eb-7a7057f0c2e2-000000@us-west-2.amazonses.com> Envelope-to: <manuel.seraphin@toussaint.de> P Received: from a27-154.smtp-out.us-west-2.amazonses.com ([54.240.27.154]:52483)     by mail.toussaint.de with esmtps  (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256    (Exim 4.97.1)   (envelope-from <010101946a8eb7f3-20e93eb5-9f40-441b-94eb-7a7057f0c2e2-000000@us-west-2.amazonses.com>)  id 1tY5E7-0000000032H-1sTw  for manuel.seraphin@toussaint.de;   Wed, 15 Jan 2025 16:22:40 +0100   X-SASI-Hits: BODYTEXTH_SIZE_3000_MORE 0.000000, BODY_SIZE_10000_PLUS 0.000000,    BODY_SIZE_25K_PLUS 0.000000, BODY_SIZE_50K_PLUS 0.000000,   BODY_SIZE_75K_PLUS 0.000000, BULK_EMAIL_SENDER 0.000000,    DKIM_SIGNATURE 0.000000, HTML_00_01 0.050000, HTML_00_10 0.050000,  KNOWN_MTA_TFX 0.000000, LEGITIMATE_SIGNS 0.000000, NO_CTA_URI_FOUND 0.000000,   NO_URI_HTTPS 0.000000, SENDER_NO_AUTH 0.000000, SUBJ_PHRASE_WATCHES 0.000000,   SUPERLONG_LINE 0.050000, SXL_IP_TFX_ESP 0.000000, SXL_IP_TFX_WM 0.000000,   __AMAZON_DKIM 0.000000, __AMAZON_MSGID 0.000000, __ANY_URI 0.000000,    __ATTACH_CTE_QUOTED_PRINTABLE 0.000000, __BODY_NO_MAILTO 0.000000,  __COURIER_PHRASE 0.000000, __CP_MEDIA_2_BODY 0.000000,  __CP_MEDIA_BODY 0.000000, __CT 0.000000, __CTYPE_HAS_BOUNDARY 0.000000,     __CTYPE_MULTIPART 0.000000, __CTYPE_MULTIPART_MIXED 0.000000,   __DQ_NEG_DOMAIN 0.000000, __DQ_NEG_HEUR 0.000000, __DQ_NEG_IP 0.000000,     __EXTORTION_PORN 0.000000, __FRAUD_URGENCY 0.000000, __FUR_HEADER 0.000000,     __FUR_IP_AMAZON 0.000000, __HAS_FROM 0.000000, __HAS_MSGID 0.000000,    __HIGHBITS 0.000000, __MIME_HTML 0.000000, __MIME_TEXT_H 0.000000,  __MIME_TEXT_H1 0.000000, __MIME_TEXT_H2 0.000000, __MIME_TEXT_P 0.000000,   __MIME_TEXT_P1 0.000000, __MIME_TEXT_P2 0.000000, __MIME_VERSION 0.000000,  __NO_HTML_TAG_RAW 0.000000, __PART_TYPE_HTML 0.000000,  __PORN_PHRASE_15_0 0.000000, __SANE_MSGID 0.000000,     __STYLE_TAGS_ATTACHED 0.000000, __SUBJ_ALPHA_END 0.000000,  __TO_MALFORMED_2 0.000000, __TO_NO_NAME 0.000000, __URI_MAILTO 0.000000,    __URI_NO_WWW 0.000000, __URI_NS 0.000000, __WEBINAR_PHRASE 0.000000   X-SASI-Probability: 8%   X-SASI-RCODE: 200   X-SASI-Version: Antispam-Engine: 5.1.4, AntispamData: 


2025.1.15.143046   DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/simple;  s=7v7vs6w47njt4pimodk5mmttbegzsi6n; d=amazonses.com; t=1736954525;  h=From:To:Message-ID:Subject:MIME-Version:Content-Type:Date:Feedback-ID;    


bh=b0FHZzxTJLOjSkXjFr1USZ3S8DGW2HC+H+znGXgZKDE=;    b=SSTiQXBudYLa1hbKrVx3qXsU8lH9YfV9O6xTr3dfkog8c+BKQphIvTMauoUecYiw  


sQ2BwB2e6SO0laNmZIfG0GerkXoPOZ0ZEEeLY30rgGRZkwfeZTtoHMY2BIoIXeS+zyu     89fcBbVOi+nDvXwz6hU6QArSJELjxnPk2y4MhMWI=)
    
    how to avoid this being in the code?
Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • Ends in question mark (2):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Markus Meyer