Avatar of Winsoup
Winsoup
 asked on

Powershell

I am looking for a powershell command that will tell me the total number of sent\received items per day per mailbox from exchange 2010. Right now I can use these to get results on an individual user but I am wondering how I would add multiple users to this?
Could I point it to a .txt file with all the user names in it?

get-messagetrackinglog -sender user@domain.com -start (get-date).adddays(-1) |select messageid -unique | measure

get-messagetrackinglog -recipient user@domain.com -start (get-date).adddays(-1) |select messageid -unique | measure

Thanks!
Powershell

Avatar of undefined
Last Comment
Winsoup

8/22/2022 - Mon
Manpreet SIngh Khatra

Try this .... the Text file should have the Alias of the users
Adams
Chrisb
Arianm

Get-Content "Location of text file" | Get-Messagetrackinglog -Sender user@domain.com -start (get-date).adddays(-1) |select messageid -unique | measure

- Rancy
Rajitha Chimmani

Small correction to the above commands and the above command will give the total number of emails again.

Have the email address of the users in the csv file with the title "emailaddress"

$FinalArray = @()
foreach($user in import-csv "csvfilelocation"){
$emails = Get-Messagetrackinglog -Sender $user.emailaddress -start (get-date).adddays(-1) |select messageid -unique | measure-Object
$Obj = New-Object PSObject -Property @{
UserEmail = $user.emailaddress
EmailCount = $emails.count}
$FinalArray += $Obj
}
}
$FinalArray | Export-csv -path "resultscsvfilelocation" -NoTypeInformation
Qlemo

Instead of collecting data in vars and dumping those at the very end, it is much better to use the streaming features of PowerShell, in particular if there is a lot of data. That allows for better memory handling, and usually speeds up things.
Further, for a single property to read from a file we do not need to use a CSV format, a simple text file is enough.
get-content "Users.txt" | foreach {
  $cnt = (Get-Messagetrackinglog -Sender $_ -start (get-date).adddays(-1) |select messageid -unique | measure-Object).Count
  New-Object PSObject @{
    UserMail = $_
    EmailCount = $cnt
  }
} | export-csv -NoType "Result.csv"

Open in new window

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Winsoup

ASKER
Qlemo, I am getting this message when your script.

Unexpected token 'New-Object' in expression or statement.
At line:1 char:195
+ get-content C:\xxxxx\xxxxxx_xxxxx\desktop\users.txt | foreach {$cn = (Get-Messagetrackinglog -Sender
ate).adddays(-1) |select messageid -unique | measure-Object).Count New-Object <<<<  PSObject @{UserMail
= $cnt}} | export-csv -NoType Result.csv
    + CategoryInfo          : ParserError: (New-Object:String) [], ParentContainsErrorRecordException
Qlemo

You need to retain the line breaks as shown. The error message shows all or parts of the code are in a single line, which won't work.
Winsoup

ASKER
I tried that and it's not doing anything, no errors or anything, just sits there like it's still running the command. I only have a few users in the text document right now for testing so I wouldn't think it would take that long.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Qlemo

Getting the tracking log can be time consuming. You will see results only at the very end, so restrict your test to a single user.
Winsoup

ASKER
I changed it to just one user but it's doing the same thing. Here is exactly what I am using so you can see if I am doing something incorrectly.

get-content C:\Users.txt | foreach {
  $cnt = (Get-Messagetrackinglog -Sender $_ -start (get-date).adddays(-1) |select messageid -unique | measure-Object).Count
  New-Object PSObject @{
    UserMail = $_
    EmailCount = $cnt
  }
} | export-csv -NoType C:\Results.csv
Qlemo

That's correct. Let's try some debugging code:
get-content C:\Users.txt | foreach {
  Get-Messagetrackinglog -Sender $_ -start (get-date).adddays(-1)
  Get-Messagetrackinglog -Sender $_ -start (get-date).adddays(-1) |select messageid -unique
  (Get-Messagetrackinglog -Sender $_ -start (get-date).adddays(-1) |select messageid -unique | measure-Object).Count
}

Open in new window

This should allow to see whether there is anything executed, and the impact of the most important operations.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Winsoup

ASKER
That said it can't find C:\users.txt
It is in that location.
Winsoup

ASKER
I just deleted the users.txt and created a new one and ran it again and it worked.
It just has a category for Count and has a number, is there a way to tell how many are sent and how many are received?
I'm guessing this is just one or the other?

I actually just added a couple more users to the users.txt and tried it and it just shows the same number under "count" for all of them so I am not sure what that number is.
And is there a way to tell what users mailbox it is in the .csv?
Qlemo

I don't know why the counts should be the same for different users. The count should be shown for sent items only, and the username should be in the CSV already. To have the received count, we have to extend the code:
get-content "Users.txt" | foreach {
  $cntsent = (Get-Messagetrackinglog -Sender $_ -start (get-date).adddays(-1) |select messageid -unique | measure-Object).Count
  $cntrcvd = (Get-Messagetrackinglog -Recipient $_ -start (get-date).adddays(-1) |select messageid -unique | measure-Object).Count
  New-Object PSObject @{
    UserMail = $_
    EmailsSent = $cntsent
    EmailsRcvd = $cntrcvd
  }
} | export-csv -NoType "Result.csv"

Open in new window

If you do not see a username in the "UserMail" column of Result.csv, that is the issue for getting the same counts for different user - there is a format/content error in users.txt, which should just contain one email address per line, and nothing else.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Winsoup

ASKER
I've attached the results that I am getting...


My text file looks like this:

user@domain.com
user@domain.com
user@domain.com
Results.xlsx
ASKER CERTIFIED SOLUTION
Qlemo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Winsoup

ASKER
That worked great!!
Thank you very much.
Winsoup

ASKER
Would it be possible to create a column that would have the date in it?  
It would just need it to be there once since it's pulling all these counts from the day before.
So the report run on 5/22/2013 would have a date of 5/21/2013.  

They are getting emailed automatically and would be nice to know what days they are for.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Qlemo

Sure. We can either name the result file:
$dt = (get-date).adddays(-1)
get-content "Users.txt" | foreach {
  $cntsent = (Get-Messagetrackinglog -Sender    $_ -start $dt |select messageid -unique | measure-Object).Count
  $cntrcvd = (Get-Messagetrackinglog -Recipient $_ -start $dt |select messageid -unique | measure-Object).Count
  New-Object PSObject -Property @{
    UserMail = $_
    EmailsSent = $cntsent
    EmailsRcvd = $cntrcvd
  }
} | select UserMail, EmailsRcvd, EmailsSent | export-csv -NoType "Result."+(Get-Date($dt) -Format yyyyMMdd)+".csv"

Open in new window

or store the date in each row:
$dt = (get-date).adddays(-1)
get-content "Users.txt" | foreach {
  $cntsent = (Get-Messagetrackinglog -Sender    $_ -start $dt |select messageid -unique | measure-Object).Count
  $cntrcvd = (Get-Messagetrackinglog -Recipient $_ -start $dt |select messageid -unique | measure-Object).Count
  New-Object PSObject -Property @{
    UserMail   = $_
    EmailsSent = $cntsent
    EmailsRcvd = $cntrcvd
    Date       = $dt.Date
  }
} | select Date, UserMail, EmailsRcvd, EmailsSent | export-csv -NoType "Result.csv"

Open in new window

Winsoup

ASKER
You rock, thank you!