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.
