Help with Get-messagetrackinglog

Posted on 2009-04-11
Last Modified: 2012-05-06
I need help with creating a script that would query the message tracking log and output to a file- wich is simple and I have thatrunning.The problem is that is that I have a large amount of aliases to query, so for now I have the code snippit below,  per alias.
Thing is I have 300 aliases to go through and the process is clumzy and slow.
Would you know how to abreviate the process?
For example, how would i use foreach in this instance?
Is there a way mabe to generate such a report( similar in output,without installing extra software like MOM)?

$StartDate = (get-date).AddDays(-2) 
$EndDate = (get-date).AddDays(-1)
get-messagetrackinglog -ResultSize Unlimited -EventID "RECEIVE" -Server "servername" -Start $StartDate -End $EndDate | format-table Sender, Timestamp, MessageSubject, EventID > e:\count\aliasname.txt
start-sleep -s 30
$aliasnamesub= Get-Content e:\count\aliasname.txt
$aliasnamesubject = $aliasnamesub.Length-5

Open in new window

Question by:Elad-a
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
LVL 71

Expert Comment

by:Chris Dent
ID: 24139381

It depends what you're working from. If you were working with aliases in a file something like this would work:

Get-Content "FileName.txt" | %{
  Get-MessageTrackinglog -ResultSize Unlimited -EventID "RECEIVE" `
    -Recipient $_ -Server "servername -Start $StartDate -End $EndDate

You'd end up with cleaner results if you could wait until the end of the search, it would allow the use of Export-CSV rather than just redirecting console output. e.g.

$Results = @()
Get-Content "FileName.txt" | %{
  $Results += Get-MessageTrackinglog -ResultSize Unlimited -EventID "RECEIVE" `
    -Recipient $_ -Server "servername -Start $StartDate -End $EndDate
$Results | Export-CSV "FileName.csv"

That's not the only way of course, it all depends how you'd like it to work or what you'd like it to work from.


Author Comment

ID: 24142721
This is the way id like to do it, but how'd i go about getting a result file per alias?
Also id like to do something like
$aa = Get-Content c:\Output.csv
but I get more lines in the count than acutlly exist.
How'd i incorporate that into your script?
Thanks for your help!
LVL 71

Expert Comment

by:Chris Dent
ID: 24145650

What's in Output.csv that you want to use?

> but I get more lines in the count than acutlly exist.

Trailing blank lines?

If it were a result file per alias I would do something like:

Get-Content "FileName.txt" | %{
  Get-MessageTrackinglog -ResultSize Unlimited -EventID "RECEIVE" `
    -Recipient $_ -Server "servername -Start $StartDate -End $EndDate `
    | Export-CSV "$_.csv"

If the passed alias were the file would be called

If the input file, "FileName.txt", is a CSV instead using Import-CSV would be more appropriate, then we would reference each field by name (assuming it has a header column).

e.g. Input file looks like:

Alias,Start,End,10/03/2009 12:33:12,11/03/2009 09:25:23

Then we could do:

Import-CSV "FileName.csv" | %{
  Get-MessageTrackinglog -ResultSize Unlimited -EventID "RECEIVE" `
    -Recipient $_.Alias -Server "servername -Start $_.Start -End $_.End `
    | Export-CSV "$($_.Alias).csv"

Office 365 Training for IT Pros

Learn how to provision Office 365 tenants, synchronize your on-premise Active Directory, and implement Single Sign-On.


Author Comment

ID: 24151389
my reference to Output.csv was to "$_.csv".
I need to do a count of mails items per alias.
Where do the trailing blank lines come from ? is there a default number of lines (in CSV fromat)?
Thanks man for your replies, its a big help.
LVL 71

Accepted Solution

Chris Dent earned 500 total points
ID: 24158471

The count for the CSV using Get-Content will be slightly out, at least 2 higher than the count of items returned (header line and a powershell line).

No default blank lines though, it should be accurate after that. However, perhaps we can do this:

Import-CSV "FileName.csv" | %{
  $Results = Get-MessageTrackinglog -ResultSize Unlimited -EventID "RECEIVE" `
    -Recipient $_.Alias -Server "servername -Start $_.Start -End $_.End
  $Results | Export-CSV "$($_.Alias).csv"
  $Results.Count >> "$($_.Alias).csv"

That should tack a result count onto the bottom of the CSV file (I hope). Is that what you're looking for?


Author Comment

ID: 24158557
Thanks Man! thats great!!

Author Closing Comment

ID: 31569187
Thanks to Chris-Dent!

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This process describes the steps required to Import and Export data from and to .pst files using Exchange 2010. We can use these steps to export data from a user to a .pst file, import data back to the same or a different user, or even import data t…
Read this checklist to learn more about the 15 things you should never include in an email signature.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
how to add IIS SMTP to handle application/Scanner relays into office 365.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question