Office 365电子邮件统计

时间:2019-05-01 23:53:27

标签: powershell

我正在尝试计算每个时期从特定电子邮件帐户发送和接收的电子邮件总数。该代码可以正常工作,但结果有些夸张。请查看下面的代码,如果它可以提供正确的结果。 我知道我只能使用$ Allmessages一次,而不先按SenderAddress和RecipientAddress进行过滤,但是它会一直运行而无需过滤。

    $Allmessages_sent = @()  
    $P = 1  
    do  
   {
    $pagedmessages_sent = Get-MessageTrace -SenderAddress $emailgroup -StartDate $StartDate -EndDate $EndDate -PageSize 1000 -Page $p `
            | Select Received,SenderAddress,RecipientAddress,Size
    $Allmessages_sent += $pagedmessages_sent 
    $p = $p + 1  
   }  
    until ($pagedmessages_sent -eq $null)  
    $senderssorted = 0;
    $senderssorted = $Allmessages_sent |group senderaddress | select @{n="SentCount";e={$_.Count}} 
    $Allmessages_rcvd = @()  
    $P_ = 1  
   do  
   {  
    $pagedmessages_rcvd = Get-MessageTrace -RecipientAddress $emailgroup -StartDate $StartDate -EndDate $EndDate -PageSize 1000 -Page $p_ `
            | Select Received,SenderAddress,RecipientAddress,Size
    $Allmessages_rcvd += $pagedmessages_rcvd
    $p_ = $p_ + 1  
    }  
    until ($pagedmessages_rcvd -eq $null)  
    $recipientsorted = 0;
    $recipientsorted = $Allmessages_rcvd |group recipientaddress | select @{n="ReceivedCount";e={$_.Count}};
    $totalSent = 0; 
    $totalRsvd = 0;
    $totalSent = $senderssorted.Sentcount;
    $totalRsvd = $recipientsorted.ReceivedCount;
    $arrayResult = @();
    $arrayResult += $totalSent;
    $arrayResult += $totalRsvd
    Write-Host "Sent: " $arrayResult[0];
    Write-Host "Received: " $arrayResult[1];

谢谢

1 个答案:

答案 0 :(得分:0)

已解决。差异说明: -总计“已收到”差异是由于计数了“失败”状态电子邮件。因此,我为“ Get-MessageTrace -SenderAddress…”和“ Get-MessageTrace -RecipientAddress”两个部分添加了-Status“已交付”。 -总计“已发送”差异归因于我无法通过Outlook的“已发送”文件夹中的设置“规则”转发的电子邮件。