![如何在 Office 365 中导出完整的 SMTP 日志](https://linux22.com/image/646350/%E5%A6%82%E4%BD%95%E5%9C%A8%20Office%20365%20%E4%B8%AD%E5%AF%BC%E5%87%BA%E5%AE%8C%E6%95%B4%E7%9A%84%20SMTP%20%E6%97%A5%E5%BF%97.png)
在 Office 365 中,是否可以导出 SMTP 日志?也许使用 powershell 或其他任何方式。
我的目标是全面了解从特定域发送和发送到特定域的所有消息。
答案1
通过结合在网上找到的两个脚本,我终于解决了这个问题。
这是完成该工作的脚本。
#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
答案2
我稍微修改了几行来收集所有日志
$OutputFile = "c:\temp\SMTPlog"
$CurrMessages | Export-Csv "$($OutputFile)$($Page).csv" -NoTypeInformation