11

In Office 365, is it possible to export the SMTP log? Maybe in powershell or any other way.

My goal is to have a complete overview of all messages send from and to a specific domain.

ZEDA-NL
  • 846
  • 1
  • 5
  • 13
  • `a complete overview of all messages send from and to a specific domain` You know, stuff like that is generally what you give up when you go to the "cloud." If you need/want that kind of full monitoring and logging, maybe cloud services aren't what you really want. – HopelessN00b Nov 19 '14 at 13:47
  • 4
    There's more to Office365 that you think HopelessN00b. :-) – ZEDA-NL Nov 19 '14 at 14:29

2 Answers2

15

By combining 2 scripts found somewhere online, I managed to figure it out.

This is the script that did the job.

#Accept input parameters 
Param( 
    [Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)] 
    [string] $Office365Username, 
    [Parameter(Position=1, Mandatory=$true, ValueFromPipeline=$true)] 
    [string] $Office365Password 
) 

$OutputFile = "DetailedMessageStats.csv" 

Write-Host "Connecting to Office 365 as $Office365Username..." 

#Connect to Office 365 
$SecurePassword = $Office365Password | ConvertTo-SecureString -AsPlainText -Force 
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Office365Username, $SecurePassword 
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://ps.outlook.com/powershell/" -Credential $cred -Authentication Basic -AllowRedirection  
Import-PSSession $session -AllowClobber 

Write-Host "Collecting Recipients..." 

#Collect all recipients from Office 365 
$Recipients = Get-Recipient -ResultSize Unlimited | select PrimarySMTPAddress 
$MailTraffic = @{} 
foreach($Recipient in $Recipients) 
{ 
    $MailTraffic[$Recipient.PrimarySMTPAddress.ToLower()] = @{} 
} 
$Recipients = $null 

#Collect Message Tracking Logs (These are broken into "pages" in Office 365 so we need to collect them all with a loop) 
$Messages = $null 
$Page = 1 
do 
{ 

    Write-Host "Collecting Message Tracking - Page $Page..." 
    $CurrMessages = Get-MessageTrace -StartDate (Get-Date).AddDays(-7) -EndDate (Get-Date)  -PageSize 5000  -Page $Page| Select Received,*Address,*IP,Subject,Status,Size

    if ($CurrMessages -ne $null)
      {
      $CurrMessages | Export-Csv C:\FILE-$PAGE.csv -NoTypeInformation
      }
    $Page++ 
    $Messages += $CurrMessages 


} 
until ($CurrMessages -eq $null) 

Remove-PSSession $session 
ZEDA-NL
  • 846
  • 1
  • 5
  • 13
  • 1
    Great script! I had to modify the URL to connect to my instance of Office 365. I used the information at [Connect to Exchange Online using remote PowerShell](https://technet.microsoft.com/en-us/library/jj984289(v=exchg.150).aspx) to handle the connection and the rest of your script to get the files. – Jason Massey Jan 23 '15 at 17:28
  • *https://practical365.com/exchange-server-protocol-logging/* applies to Office365 ? How-to configure Office365 to Troubleshooting Email Delivery and get logs ? – Kiquenet Jun 03 '22 at 06:55
-1

I made a slight change to a few lines to colect all the logs

$OutputFile = "c:\temp\SMTPlog"

$CurrMessages | Export-Csv "$($OutputFile)$($Page).csv" -NoTypeInformation
Fred B
  • 1
  • 3
    Hi, I suspect you wanted to edit the answer to add that detail or to leave a comment on the other answer, if you intend to leave it as an answer I suggest to paste the entire script with your change and leave a note that it’s based on the other answer, so it would be a complete answer. – yagmoth555 Oct 24 '18 at 21:53