Powershell Script to export all e-mail address transmitted by the server (To and From).

This section contains user-submitted tutorials.
Post Reply
pauloroberto
New user
New user
Posts: 5
Joined: 2018-06-15 16:07

Powershell Script to export all e-mail address transmitted by the server (To and From).

Post by pauloroberto » 2018-06-15 16:25

Hello people,
Here's my first post, hope this helps someone and forgive my bad english.

A costumer asked me to export every e-mail that pass on the server,
I made a script that filter the log and export the result on a CSV file.

Requirement:
At least Powershell V3
Application log enabled on the Logging options.

Code: Select all

<#
Script criado por Paulo Roberto (pauloroberto [arroba] conectadores [ponto] com [ponto] br)

Filtra o log do servidor de e-mail.

#>

#Edit the folder to save the CSV files.
$Planilha="D:\Folder to save\Log-$(get-date -Format yyyyMMdd).csv"

#Edit if not installed on default Folder
$ArquivoLog="C:\Program Files (x86)\HMailServer\Logs\hmailserver_$(get-date -format yyyy-MM-dd).log"

if (!(Test-Path $Planilha))
    {
    "`"From`",`"To`"" | Out-File $Planilha
    }

gc $ArquivoLog | where {$_ -like "`"APPLICATION`"*SMTPDeliverer*Delivering message*"} | ForEach-Object `
    {
    $log=$_

    $de=([regex]::Match($log,"(?<= from )(.*)(?= to )")).value
    $para=([regex]::Match($log,"(?<= to )(.*)(?=\. File)")).value

    "`"$From`",`"$To`"" | Out-File $Planilha -Append

    }
    
You can make this script run daily via Schedule Task.
You can read the result with any CSV compatible reader, including the following Powershell line: import-csv "File.csv" | out-gridview

Next post I will create a script that read the generated file and shows you the number of e-mail sent by any account on the server,
and, if desired, disable that account after a pre-defined value.


See you.

User avatar
SorenR
Senior user
Senior user
Posts: 3227
Joined: 2006-08-21 15:38
Location: Denmark

Re: Powershell Script to export all e-mail address transmitted by the server (To and From).

Post by SorenR » 2018-06-15 17:30

hMailServer can log traffic in AWStat format. Check it out, it could have saved you a lot of work :mrgreen:
SørenR.

“With age comes wisdom, but sometimes age comes alone.”
- Oscar Wilde

pauloroberto
New user
New user
Posts: 5
Joined: 2018-06-15 16:07

Re: Powershell Script to export all e-mail address transmitted by the server (To and From).

Post by pauloroberto » 2018-06-15 20:30

It wasn't hard...
Anyway, good tip, I will check, thanks.

Post Reply