1

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
David
  • 15,150
  • 15
  • 61
  • 83
  • ...what's the question? – gvee May 09 '17 at 10:48
  • The bit that says hard coding for several days, I want to set StartTime to -180 days and it to "Just Work" TM – David May 09 '17 at 11:05
  • Try changing the `-7` to `-180`..? – gvee May 09 '17 at 11:09
  • And then I have to write 180 lines of [Int]$Day0Mail =, [Int]$Day1Mail =, [Int]$Day2Mail =, [Int]$Day3Mail = ??? And then how to put these numbers into the $MailObj and concatenate to easily output via Excel/CSV. – David May 09 '17 at 11:11
  • Aha! And now your requirements have become clearer! As the current answer suggests: you need a loop. – gvee May 09 '17 at 12:24

1 Answers1

0

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
}
Mark Wragg
  • 22,105
  • 7
  • 39
  • 68
  • I'm sorry Mark but the point is to automate the [Int]$Day0Mail, [Int]$Day1Mail, [Int]$Day2Mail, [Int]$Day3Mail etc – David May 09 '17 at 11:12
  • OK, just needs a simple loop, i'll amend the answer. – Mark Wragg May 09 '17 at 11:13
  • Added a potential solution but it's untested. Try it and see, i'll need to mock up a POC to test it my side. – Mark Wragg May 09 '17 at 11:19
  • 1
    Your code didn't show how `$ArrayStartDates` was populated but i've made an assumption. It's worth also noting there might be a more elegant solution than this, but i'm working within the constraints of your existing script to (hopefully) make it easier to understand. – Mark Wragg May 09 '17 at 11:29
  • Afraid it just outputs 1 day – David May 09 '17 at 12:54
  • You're welcome :) be great if you could upvote also, if it helped you. Also if there was a bug in my solution feel free to edit in the fix. – Mark Wragg May 09 '17 at 13:30