bayard0
bayard0

Reputation: 27

PowerShell script with specific date/time range

I am trying to run a powershell script for exchange 2010 to pull information from a specific date/time range.

example: show me amount of received emails from monday-friday time range 6am-11pm

get-messagetrackinglog -resultsize unlimited -Recipient [email protected] -Server EXCHANGE -Start "3/4/2013 6:00:00 AM" -End "3/6/2013 23:00:00 PM" | select messageid -unique | measure

but I would like to make the date range not so static. so If i run the script at 11pm on Friday night, every week, how can i get it to do this query for the last 5 days.

I was trying adding in (get-date).adddays(-5) but I can't figure out how to add that in.

any help will be greatly appreciated.

Upvotes: 2

Views: 6674

Answers (3)

Adil Hindistan
Adil Hindistan

Reputation: 6605

Just trying to answer the part about the past date and hour and assuming you are running this at 11pm but want to go back to 6am (17hrs diff)... Maybe use something like:

$past=(Get-Date).adddays(-5).addhours(-17)

And then try

-start $past -end (get-date) 

Upvotes: 0

Neossian
Neossian

Reputation: 705

Try this

for($i=-4,$i -lt 0,$i++){
    $start = (get-date -hour 6).adddays($i);
    $end = (get-date -hour 23).adddays($i);
    Write-host $start.DayoftheWeek  (get-messagetrackinglog -resultsize unlimited -Recipient [email protected] -Server EXCHANGE -Start $start -End $end | select messageid -unique | measure).count 
}

Upvotes: 1

mjolinor
mjolinor

Reputation: 68263

The get-messagetrackinglog cmdlet only takes a single argument for -start and -end, so you can't specify that in a single command.

You can run 5 separate queries of the messagetracking logs from 6AM-11PM, each on different days and aggregate those results together, or you can do one query for all the logs from 6AM on the first day to 11PM on the last day, then filter out the ones that are timestamped between 11PM and 6AM in the interim days.

Upvotes: 0

Related Questions