Tentacles of the Technoctopus

Just blagging my thoughts on the web

Menu
  • About
    • Gifts Baby… Gifts
  • Strikebreaking
Menu

Email Usage Report for Exchange 2007

Posted on June 3, 2010November 27, 2010 by admin

So in a post that has NOTHING to do with anything else I’ve posted on here, I thought I’d share a piece of PowerShell/Exchange scripting I ended up writing for work.  I’ll update this post as I make changes, but here’s the code.  Feel free to use or modify as needed.  Please note this was written in PowerShell 1, and if you destroy your exchange implementation fiddling around with this, I take 0 responsibility.

Step 1:

Make a batch file.  This file will be used so that you can easily execute your script, or use Windows Task Scheduling to automate when this script is run.  To do this just open a text editor and save a file with the .bat extension.  You will want one line inside of the file as follows

PowerShell.exe -PSConsoleFile “C:\Program Files\Microsoft\Exchange Server\Bin\ExShell.psc1” -Command “c:\mv2.ps1”

Please note that I’m using mv2.ps1 as my actual PowerShell file, you should change this path to point to wherever you’re saving the powershell file itself.

2) Make the powershell file!  You can just do another notepad file and save it with whatever name you use in the batch file.  Then paste the contents as below, with whatever appropriate edits

#The AddBreaks Function will format each user’s emails and present them in readable/HTML format
function AddBreak($MessageLog){
$MessageBody = “<table cellpadding=’4′><tr><td><b>Timestamp</b></td><td width=500><b>MessageSubject</b></td><td><b>Recipients</b></td></tr>”

forEach($rowItem in $MessageLog){
$MessageBody = $MessageBody + “<tr>”
$MessageBody = $MessageBody + “<td>” + $rowItem.Timestamp + “</td>”
$MessageBody = $MessageBody + “<td>” + $rowItem.MessageSubject + “</td>”
$MessageBody = $MessageBody + “<td>” + $rowItem.Recipients + “</td>”
$MessageBody = $MessageBody + “</tr>”
}
$MessageBody = $MessageBody + “</table>”
$MessageBody | out-file c:\temp.txt -append
$MessageBody = “”
}

#The Start and End variables set the period of this report, currently this displays the previous day
$MessageBody = “”
$Start = (Get-Date -Hour 00 -Minute 00 -Second 00).AddDays(-1)
$End = (Get-Date -Hour 23 -Minute 59 -Second 59).AddDays(-1)

$sender = “youremailaddress@email.com”
$recipient = “recipientemailaddress@email.com”
$CC = “ccifneeded@email.com”
$BCC = “bccifneeded@email.com”
$mailserver = “localhost”
$subject = “Email Report for $Start”
#This above block sets the email header and should be changed as required.

“<h1>Email Usage Report</h1>Please send all problems or new user requests to youremailaddress@email.com.  <br><br>” | out-file c:\temp.txt

#Complete one of these blocks for each individual you wish to have included in the report
“<br>User One<b>Sent</b> Messages: <br>” |  out-file c:\temp.txt -append
$User1Sent = get-messagetrackinglog -Sender “user.one@email.com” -EventID “SEND” -Start $Start -End $End | select Timestamp, MessageSubject,@{n=”Recipients”;e={[string]::join(“,”,$_.recipients)}}
AddBreak($User1Sent)

“<br>User One <b>Received</b> Messages: <br>” |  out-file c:\temp.txt -append
$User1Received = get-messagetrackinglog -Sender “user.one@email.com” -EventID “RECEIVE” -Start $Start -End $End | select Timestamp, MessageSubject,@{n=”Recipients”;e={[string]::join(“,”,$_.recipients)}}
AddBreak($User1Received)
#Be sure to increment the user counter and use new variables for each user.

“<br>User Two<b>Sent</b> Messages: <br>” |  out-file c:\temp.txt -append
$User2Sent = get-messagetrackinglog -Sender “user.two@email.com” -EventID “SEND” -Start $Start -End $End | select Timestamp, MessageSubject,@{n=”Recipients”;e={[string]::join(“,”,$_.recipients)}}
AddBreak($User2Sent)

“<br>User Two <b>Received</b> Messages: <br>” |  out-file c:\temp.txt -append
$User2Received = get-messagetrackinglog -Sender “user.two@email.com” -EventID “RECEIVE” -Start $Start -End $End | select Timestamp, MessageSubject,@{n=”Recipients”;e={[string]::join(“,”,$_.recipients)}}
AddBreak($User2Received)
#Here’s an example block of a second user’s email being added.  Add as many of these blocks as you need

$body = get-content c:\temp.txt
$msg = new-object System.Net.Mail.MailMessage $sender, $recipient, $subject, $body
#Uncomment the line below if you want to use attachments
#$attachment = new-object System.Net.Mail.Attachment c:\temp5.csv
$msg.CC.Add($CC)
#$msg.BCC.Add($BCC)
#Uncomment the line below if you wish to use attachments
#$msg.Attachments.Add($attachment)
$msg.IsBodyHTML = $true
$client = new-object System.Net.Mail.SmtpClient $mailserver
$client.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
$client.Send($msg)

3) The above script uses a text file to parse all this information, just  create an empty text file in a location you have write access to.  The above script uses c:\temp.txt

4) You can just run the batch file to test that everything’s working, or use Windows Task Scheduler to set this up to run on a daily basis.

5) Leave a comment here if this helps!  I hacked together this code from a ton of places, with a ton of people’s help.  I hope if you have success or improvements you’ll let me know and hopefully this helps make some people’s life easier.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Tentacles of the Technoctopus

Wherein I write about whatever's on my mind in the hopes that I find a little bit of common ground with the other spacemen hurtling around this damn place.

The Past!

Blogroll

  • Cool Design and Food Site
  • Get me a Gift
  • Interesting Random Blog I Found
  • Straight to Heck
  • Tasha’s Getting Married
  • The Call to Adventure
  • TJQ'S BBQ(blogbque?)

Tags

a poem about dreaming and dying A slow downward spiral blood for the blood god Boooring Depressing Everybody writes poetry these days exchange 2007 Food Review fuck the japanese are weird General genius ideas Gluttony is my favorite sin Henry Phillips Hitting on Random Chicks Horror Journalism Houston how do you spell existentialism? hurricane ike I'm not the World Champion of Anything I almost killed myself with fire I am Trader Joe's Bitch I didn't write it but I dig it I didn't write this Life Life Updates Living in Interesting Times mark lewis Movie Review Music Review My Budding Comedic Career Nathan's an Idiot nerdville Nobody Cares Punching the Clown Retarded People Exploding review Sci-Fi is Dead Sleeping storytelling The Japanese are Fucking Weird Things to do in Los Angeles while you're fucked up Weird Dreams Wherein I almost die Workin' in LA Writing

Meta

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org
© 2025 Tentacles of the Technoctopus | Theme by Superbthemes