1

我正在尝试从电子邮件地址列表中获取收到的电子邮件的月度报告。我想将结果导出到 .csv 并在可能的情况下通过电子邮件自动将 csv 发送到另一个地址。

这是我在交换管理外壳中的方法,看起来像这样,但没有在 .csv 中创建任何数据

    $mail = @("mymail@mydomain.com","another@mydomain.com")
$(foreach ($name in $mail)
{
    $startofmonth = Get-Date -format MM/dd/yyyy -day 1 -hour 0 -minute 0 -second 0
    $endofmonth = ((Get-Date -day 1 -hour 0 -minute 0 -second 0).AddMonths(1).AddSeconds(-1))
    $endofmonth = "{0:MM/dd/yy}" -f [datetime]$endofmonth

    $results = (Get-MessageTrackingLog -ResultSize unlimited -recipients $name -Start $startofmonth -End $endofmonth -EventId RECEIVE).count
    $results
}) | Export-CSV -path \\mymachine\c$\output.csv -NoTypeInformation

如何在 .csv 中获取我想要的数据:

邮件地址 | 计数(收到的邮件) | 大体时间

4

2 回答 2

2

您可以为每个结果创建一个自定义对象并将其返回给一个数组$output。然后将数组转换为CSV格式并输出到文件中。

$mail = @("mymail@mydomain.com","another@mydomain.com")
$startofmonth = Get-Date -format MM/dd/yyyy -day 1 -hour 0 -minute 0 -second 0
$endofmonth = ((Get-Date -day 1 -hour 0 -minute 0 -second 0).AddMonths(1).AddSeconds(-1))
$endofmonth = "{0:MM/dd/yy}" -f [datetime]$endofmonth

[Array]$output = foreach ($name in $mail) {

    $results = (Get-MessageTrackingLog -ResultSize unlimited -recipients $name -Start $startofmonth -End $endofmonth -EventId RECEIVE).count

    [pscustomobject]@{
        Name=$name;
        Received=$results;
        StartTime=$startofmonth;
        EndTime=$endofmonth;
    }
}

$output | ConvertTo-Csv -NoTypeInformation | Out-File -FilePath \\mymachine\c$\output.csv -Append 

这将返回一个CSV如下所示的文件:

"Name","Received","StartTime","EndTime"
"mymail@mydomain.com","222","10/01/2016","10/31/16"
"another@mydomain.com","340","10/01/2016","10/31/16"
于 2016-10-06T10:28:05.587 回答
0

尝试一下:

 $mails = ("mymail@mydomain.com","another@mydomain.com")
 $startofmonth = Get-Date -format MM/dd/yyyy -day 1 -hour 0 -minute 0 -second 0
 $endofmonth = ((Get-Date -day 1 -hour 0 -minute 0 -second 0).AddMonths(1).AddSeconds(-1))
 $endofmonth = "{0:MM/dd/yy}" -f [datetime]$endofmonth


 Get-MessageTrackingLog -ResultSize Unlimited -Start $startofmonth -End  $endofmonth -EventId RECEIVE | where{$_.Recipients -in   $mails} | select- object  Timestamp,SourceContext,Source,EventId,MessageSubject,Sender, {$_.Recipients} | 
group-object -property Timestamp |
export-csv C:\temp\MessageTrackingLogResults.csv
于 2016-10-06T10:32:03.403 回答