31 lines
1.3 KiB
PowerShell
31 lines
1.3 KiB
PowerShell
# PowerShell script to list senders of emails from an on-premise Exchange server for the current day.
|
|
|
|
# Set the start and end times for today
|
|
$today = Get-Date -Hour 0 -Minute 0 -Second 0
|
|
$tomorrow = (Get-Date).AddDays(1).Date
|
|
|
|
Write-Host "Searching for emails sent between $($today) and $($tomorrow)..."
|
|
|
|
try {
|
|
# Get message tracking logs for sent emails within today's date range
|
|
# Filtering by EventID 'SEND' focuses on emails leaving the server or being sent internally.
|
|
# Adjust -ResultSize as needed; 'Unlimited' retrieves all, but can be slow for large environments.
|
|
$sentEmails = Get-MessageTrackingLog -Start $today -End $tomorrow -EventId "SEND" -ResultSize Unlimited |
|
|
Select-Object -ExpandProperty Sender |
|
|
Sort-Object -Unique
|
|
|
|
if ($sentEmails) {
|
|
Write-Host "`nSenders of emails today:`n"
|
|
$sentEmails | ForEach-Object {
|
|
Write-Host $_
|
|
}
|
|
Write-Host "`nTotal unique senders: $($sentEmails.Count)"
|
|
} else {
|
|
Write-Host "`nNo emails sent today found."
|
|
}
|
|
}
|
|
catch {
|
|
Write-Error "An error occurred while retrieving message tracking logs: $($_.Exception.Message)"
|
|
Write-Error "Please ensure you have the necessary Exchange management tools installed and are running the script with appropriate permissions."
|
|
}
|