powershellexchange-serverexchange-server-2013

How to get number of emails sent daily via Exchange Server 2013


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

Solution

  • You have a couple of options. You could just put a Param() block around these:

    Param(
        $StartTime = (Get-Date -Hour 00 -Minute 00 -Second 00).AddDays(-7),
        $EndTime = (Get-Date -Hour 23 -Minute 59 -Second 59).AddDays(-1)
    )
    

    Which would keep your defaults but if you wanted to override them you would run your script like this:

    .\YourScript.ps1 -StartTime 01/02/2017 -EndTime (get-date)
    

    Or if you just want to flexible on the number of days previous it gathers data for, add a Param block like this above your existing code:

    Param($Days = 7)
    

    And then change this line as follows:

    $StartTime = (Get-Date -Hour 00 -Minute 00 -Second 00).AddDays(-$Days)
    

    And again, run the script like this:

    .\YourScript.ps1 -Days 180
    

    Or, if you don't want to do this using parameters, you could use the $Days solution but with Read-Host instead:

    $Days = Read-Host "How many days history do you want?"
    $StartTime = (Get-Date -Hour 00 -Minute 00 -Second 00).AddDays(-$Days)
    

    Per the comments, to deal with the other duplicating parts of your code, make sure you have a $Days parameter and then you could add an inner ForEach loop like this:

    $MailObj = New-Object -TypeName psobject -Property ([Ordered]@{
                Name = $Email.Name
                Email = $Email.Email
            })
    
    0..$Days | ForEach-Object {
        $DayMail = 0
        $Day = $_
        [int]$DayMail = ($MTL | Where-Object {($_.Timestamp -gt $ArrayStartDates[$Day]) -And ($_.Timestamp -lt (Get-Date $ArrayStartDates[$Day] -Hour 23 -Minute 59 -Second 59))}).Count
        $MailObj | Add-Member -MemberType NoteProperty -Name ($ArrayStartDates[$Day].ToShortDateString()) -Value $DayMail
    }