Skip to content
www.mytechnote.ru
Menu
  • Administration
    • Scripts
  • Equipment
    • Reviews
    • Instructions
  • Other
  • English
    • English
    • Русский
Menu

Analyzing SMTP Logs in Microsoft Exchange with PowerShell

Some time ago, it became necessary to significantly modify the default Microsoft Exchange 2016 connectors related to authentication. After making these changes, it was essential to ensure they were functioning correctly and identify potentially problematic clients. To achieve this, I created a small script to analyze logs from all Exchange servers in the organization. I decided to save it here, as it can be useful to have an example of large-scale data collection from multiple servers at hand.

Main Functions of the Script:

Log Collection: Analyzes the latest 5–20 log files from directories containing SMTP logs (SmtpReceive, SmtpSend).

Event Filtering: Searches for lines containing key patterns (AUTH, Frontend, Unrecognized authentication, etc.).

Local Connection Filtering: Excludes lines with external IP addresses, allowing a focus on analyzing local clients.

Detection of Clients on Custom Connectors: Helps determine which devices or applications are connecting via non-standard connectors.

Remote Execution: The script runs on all Exchange servers using Invoke-Command.

Usage:

Running this script helps quickly identify suspicious login attempts, authentication failures, and possible issues with SMTP clients. Such monitoring is valuable for Exchange administrators ensuring the security and stability of the mail server.

Examples of scripts with different filters and outputs:

$servers = get-exchangeserver

$ScriptBlock = {
    $paths = @(
        "C:\Program Files\Microsoft\Exchange Server\V15\TransportRoles\Logs\FrontEnd\ProtocolLog\SmtpReceive",
        "C:\Program Files\Microsoft\Exchange Server\V15\TransportRoles\Logs\FrontEnd\ProtocolLog\SmtpSend",
        "C:\Program Files\Microsoft\Exchange Server\V15\TransportRoles\Logs\Hub\ProtocolLog\SmtpReceive",
        "C:\Program Files\Microsoft\Exchange Server\V15\TransportRoles\Logs\Hub\ProtocolLog\SmtpSend"
    )
    
    # array of patterns
    $patterns = @(",AUTH", "LOGIN", "Frontend")

    # Regexp for local ip addresses (ignore non local)
    $localIpRegex = "^(10\.)|(172\.(1[6-9]|2[0-9]|3[01]))|(192\.168\.)|(127\.)"

    $results = foreach ($path in $paths) {
        $logs = Get-ChildItem -Path $path -Recurse | Sort-Object LastWriteTime -Descending | Select-Object -First 5
        foreach ($log in $logs) {
            $matches = Select-String -Path $log.FullName -Pattern $patterns[0] -Context 0, 4
            foreach ($match in $matches) {
                $containsAllPatterns = $true
                foreach ($pattern in $patterns) {
                    if ($match.Line -notmatch $pattern) {
                        $containsAllPatterns = $false
                        break
                    }
                }

                # Check if there local addresses
                $containsNonLocalIp = $false
                if ($containsAllPatterns) {
                    # check all ips
                    $ipAddresses = [System.Text.RegularExpressions.Regex]::Matches($match.Line, "\b(?:\d{1,3}\.){3}\d{1,3}\b")
                    foreach ($ip in $ipAddresses) {
                        if ($ip.Value -notmatch $localIpRegex) {
                            $containsNonLocalIp = $true
                            break
                        }
                    }
                }

                # show the string if everything is fine
                if ($containsAllPatterns -and -not $containsNonLocalIp) {
                    $match.Line
                    $match.Context.PostContext
                }
            }
        }
    }

    if ($results -ne $null) {
        Write-Host $env:computername
        $results -join "`n" 
        Write-Host ""
    }
}

foreach ($server in $servers) {
    Invoke-Command -ComputerName $server.name -ScriptBlock $ScriptBlock
}







$servers = get-exchangeserver

$ScriptBlock = {
    $paths = @(
        "C:\Program Files\Microsoft\Exchange Server\V15\TransportRoles\Logs\FrontEnd\ProtocolLog\SmtpReceive",
        "C:\Program Files\Microsoft\Exchange Server\V15\TransportRoles\Logs\FrontEnd\ProtocolLog\SmtpSend",
        "C:\Program Files\Microsoft\Exchange Server\V15\TransportRoles\Logs\Hub\ProtocolLog\SmtpReceive",
        "C:\Program Files\Microsoft\Exchange Server\V15\TransportRoles\Logs\Hub\ProtocolLog\SmtpSend"
    )
    
    $patterns = @(",AUTH", "Frontend")

    $localIpRegex = "^(10\.)|(172\.(1[6-9]|2[0-9]|3[01]))|(192\.168\.)|(127\.)"

    $results = foreach ($path in $paths) {
        $logs = Get-ChildItem -Path $path -Recurse | Sort-Object LastWriteTime -Descending | Select-Object -First 20
        foreach ($log in $logs) {
            $matches = Select-String -Path $log.FullName -Pattern $patterns[0]
            foreach ($match in $matches) {
                $containsAllPatterns = $true
                foreach ($pattern in $patterns) {
                    if ($match.Line -notmatch $pattern) {
                        $containsAllPatterns = $false
                        break
                    }
                }

                $containsNonLocalIp = $false
                if ($containsAllPatterns) {
                    $ipAddresses = [System.Text.RegularExpressions.Regex]::Matches($match.Line, "\b(?:\d{1,3}\.){3}\d{1,3}\b")
                    foreach ($ip in $ipAddresses) {
                        if ($ip.Value -notmatch $localIpRegex) {
                            $containsNonLocalIp = $true
                            break
                        }
                    }
                }

                if ($containsAllPatterns -and -not $containsNonLocalIp -and $match.Line -notmatch "10.227.96.239" -and $match.Line -notmatch "192.168.12.180" -and $match.Line -notmatch "10.0.4.85" -and $match.Line -notmatch "10.225.76.50"){
				    $match.Line
                }
            }
        }
    }
    
    if ($results -ne $null) {
        Write-Host $env:computername
        $results -join "`n"
        Write-Host ""
    }
}

foreach ($server in $servers) {
    Invoke-Command -ComputerName $server.name -ScriptBlock $ScriptBlock
}



$ScriptBlock = {
    $paths = @(
        "C:\Program Files\Microsoft\Exchange Server\V15\TransportRoles\Logs\FrontEnd\ProtocolLog\SmtpReceive",
        "C:\Program Files\Microsoft\Exchange Server\V15\TransportRoles\Logs\FrontEnd\ProtocolLog\SmtpSend",
        "C:\Program Files\Microsoft\Exchange Server\V15\TransportRoles\Logs\Hub\ProtocolLog\SmtpReceive",
        "C:\Program Files\Microsoft\Exchange Server\V15\TransportRoles\Logs\Hub\ProtocolLog\SmtpSend"
    )
	$results = foreach ($path in $paths) {
		$logs = Get-ChildItem -Path $path -Recurse | Sort-Object LastWriteTime -Descending | Select-Object -First 20
		foreach ($log in $logs) {
			$matches = Select-String -Path $log.FullName -Pattern "Unrecognized authentication"
			foreach ($match in $matches) {
				$match.Line  
			}
		}
	}
	if ($results -ne $null){
	$env:computername
    $results -join "`n"  # print results
    Write-Host ""
	}
}
foreach ($server in $servers) {
    Invoke-Command -ComputerName $server.name -ScriptBlock $ScriptBlock
}

Output:

The script displays the server name and log entries matching the specified criteria, making analysis and troubleshooting easier. It was initially written as a quick solution and is shared here as a foundation for building a more refined and useful tool in the future.

Posted on 23.03.202523.03.2025 by sanglyb

Post navigation

← How to Manage Roles in Exchange: Useful Commands and Instructions
How to Audit Seafile: SQL Scripts to Get User, Repository and Access Information →

Leave a Reply Cancel reply

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

Подписывайтесь

Youtube
vk
telegram
github
twitter
rss
  • Форум
  • Об авторе
  • Поддержать автора

Популярные записи

Новые записи

  • How to Extend an LVM Partition in Linux
  • SCCM Reporting Errors After Migrating from SQL Server 2016 to 2019
  • Problem Using PassFiltEx on Older CPUs
  • How to remove attachments from old calendar events in Exchange using a script
  • How to download and install Office 2024 LTSC

Архивы

  • 2026 (3)
  • 2025 (5)
  • 2024 (7)
  • 2023 (30)
  • 2022 (4)
  • 2020 (17)
  • 2017 (82)
  • 2016 (5)
© 2015-2026 www.mytechnote.ru
Использование материалов сайта возможно только с разрешения владельца или обязательна индексируемая, прямая, передающая вес ссылка на материал.