Powershell

如何在 Office 365 中導出完整的 SMTP 日誌

  • March 10, 2021

在 Office 365 中,是否可以導出 SMTP 日誌?也許在 powershell 或任何其他方式。

我的目標是全面了解從特定域發送和發送到特定域的所有消息。

通過結合在網上某處找到的 2 個腳本,我設法弄明白了。

這是完成這項工作的腳本。

#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 

引用自:https://serverfault.com/questions/645484