Powershell to get count of mail messages during 1 month

Hi , I have a script that normally count the send / receive messages on my exchange 2010.

But I get no count results in the output  .

Can someone help me out ?

#REQUIRES -Version 2.0

<#
.SYNOPSIS 
Count the total emails sent and received per user in the current month

.DESCRIPTION
Create a schedule task with triggers : "Monthly + last day"
The script will run every last day and retrieve informations since the first day of the month

.NOTES  
NAME: TotalEmailsSentReceivedPerDay.ps1
AUTHOR: ServCat
v1.2 - 03.01.14 : Send results by email
v1.1 - 02.12.14 : Update script with ConvertTo-HTML
v1.0 - 01.31.14 : Create the script

.LINK 
Script posted over : www.ServCat.be

.EXAMPLE 
Just run ./CountSentRecMailsPerUser.ps1
#>

############ Start Import the Exchange 2013 modules if available, otherwise import 2007.
if (Get-PSSnapin Microsoft.Exchange.Management.PowerShell.E2013 -Registered -ErrorAction SilentlyContinue) {
    Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2013


############ Start Variables
[Int] $intSent = $intRec = 0
$emails = Get-Mailbox
$StartDate = get-date -uformat "%m.01.%Y"  ## Be careful => English date format
$EndDate = get-date -uformat "%m.%d.%Y"    ## Be careful => English date format
$tab2 = @()
$tabInfo = @()
############ End variables

############ Start HTML Style
$head = @'
<style>
body { background-color:#FFFFFF;
       font-family:Tahoma;
       font-size:11pt; }
td, th { border:1px solid black; 
         border-collapse:collapse;
text-align:center;
background+color:#e0e0e0;
width:300px;}
th { color:#ffffff;
     background-color:#20a000;
text-align:center;}
table, tr, td, th { padding: 1px; margin: 0px }
table { margin-left:15px; }
</style>
'@
############ End HTML Style

############ Start retrieve email address + NB sent/received mails
foreach ($i in $emails) {

$intRec = 0                       #Number of received mails
$intSent = 0                      #Number of sent mails
$address = $i.PrimarySmtpAddress  #Email address
$address = $address.ToString()    #Email address to string
$object = new-object Psobject     #Create the object
$objectInfo = new-object Psobject  #Create the object info

############ Sent mails
Get-TransportServer | Get-MessageTrackingLog -ResultSize Unlimited -Start $StartDate -End $EndDate -Sender $address -EventID RECEIVE | ? {$_.Source -eq "STOREDRIVER"} | ForEach { $intSent++ }

############ Received mails
Get-TransportServer | Get-MessageTrackingLog -ResultSize Unlimited -Start $StartDate -End $EndDate -Recipients $address -EventID DELIVER | ForEach { $intRec++ }

############ Insert address + number of sent/received mails
$object | Add-member -Name "User" -Membertype "Noteproperty" -Value $address
$object | Add-member -Name "Received" -Membertype "Noteproperty" -Value $intRec
$object | Add-member -Name "Sent" -Membertype "Noteproperty" -Value $intSent
$tab2 += $object
}

############ Insert informations
$objectInfo | Add-member -Name "Title" -Membertype "Noteproperty" -Value "Stats Mails"
$objectInfo | Add-member -Name "Version" -Membertype "Noteproperty" -Value "v1.0"
$objectInfo | Add-member -Name "Author" -Membertype "Noteproperty" -Value "ServCat"
$tabInfo += $objectInfo

############ Sort by number of sent emails
$tab2 = $tab2 | Sort-Object Sent -descending

############ ConvertTo-HTML
$body =  $tabInfo | ConvertTo-HTML -head $head
$body += $tab2 | ConvertTo-HTML -head $head

############ Send emails with results
send-mailmessage -to "kristof@servcat.be" -from "StatMails@servcat.be" -subject "Stats mails From $StartDate To $EndDate" -body ($body | out-string) -BodyAsHTML -SmtpServer "mail.servcat.be"

############ end of Script

April 28th, 2015 9:21am

You want us to walk through 96 lines of your script and figure out why its not working for you?  Instead, how about you do this yourself, and come back when you have a specific question.  Or, use the numerous scripts that already exist for this purpose:

Free Windows Admin Tool Kit Click here and download it now
April 28th, 2015 2:01pm

Hi Skiwie,

Except above suggestion, I recommend you post this in Powershell Forum also, as they should have more professional knowledge on scripting and you may get effective solution timely.

Best regards,

April 29th, 2015 1:28am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics