我正在尝试从电子邮件地址列表中获取收到的电子邮件的月度报告。我想将结果导出到.csv,如果可能的话,通过电子邮件自动发送csv到另一个地址。
这是我在exchange-management-shell中的方法看起来像这样,但是没有在.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中获取我想要的数据:
mailaddress |计数(收到邮件)|时限
答案 0 :(得分: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"
答案 1 :(得分: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