I'm writing a powershell script that queries our Exchange server for the daily number of SENT emails over a list of several days.
I would like to output the results in an excel file so I can do so more analysis (pivot tables etc).
The issue I have is I'm hard coding several days but want this to be flexible to run it over several months.
$StartTime = (Get-Date -Hour 00 -Minute 00 -Second 00).AddDays(-7),
$EndTime = (Get-Date -Hour 23 -Minute 59 -Second 59).AddDays(-1),
ForEach ($Email in $FilterArr) {
$MTL = Get-MessageTrackingLog -Start $StartTime -End $EndTime -EventId SEND -ResultSize Unlimited -Sender $Email.Email
[Int]$Day0Mail = ($MTL | Where-Object {($_.Timestamp -gt $ArrayStartDates[0]) -And ($_.Timestamp -lt (Get-Date $ArrayStartDates[0] -Hour 23 -Minute 59 -Second 59))}).Count
[Int]$Day1Mail = ($MTL | Where-Object {($_.Timestamp -gt $ArrayStartDates[1]) -And ($_.Timestamp -lt (Get-Date $ArrayStartDates[1] -Hour 23 -Minute 59 -Second 59))}).Count
...
$MailObj = New-Object -TypeName psobject -Property ([Ordered]@{
Name = $Email.Name
Email = $Email.Email
[String]$ArrayStartDates[0].ToShortDateString() = $Day0Mail
[String]$ArrayStartDates[1].ToShortDateString() = $Day1Mail
....
$ReportArr += $MailObj
}
$ReportArr | Export-Csv -NoTypeInformation -Path $ReportPath -Force