{"id":15441,"date":"2011-03-02T00:01:00","date_gmt":"2011-03-02T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/03\/02\/use-powershell-to-track-email-messages-in-exchange-server\/"},"modified":"2011-03-02T00:01:00","modified_gmt":"2011-03-02T00:01:00","slug":"use-powershell-to-track-email-messages-in-exchange-server","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-track-email-messages-in-exchange-server\/","title":{"rendered":"Use PowerShell to Track Email Messages in Exchange Server"},"content":{"rendered":"<p><b>Summary<\/b>: Learn how to use Windows PowerShell to evaluate email messages that are sent and received on Microsoft Exchange Server.\n<img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Question\" border=\"0\" title=\"Hey, Scripting Guy! Question\">\nHey, Scripting Guy! I need to be able to create a report that tells my boss information about email messages that are sent and received by our Exchange Server. Can I use Windows PowerShell to do this?\n&mdash;CR\n<img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Answer\" border=\"0\" title=\"Hey, Scripting Guy! Answer\"> Hello CR,\nMicrosoft Scripting Guy, Ed Wilson, here. Our guest blogger today is Rob Campbell. Rob recently was our guest in February, and he is back again today to tell us the answer to your question. You can read Rob&rsquo;s previous post and his bio in <a target=\"_blank\" href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/02\/18\/speed-up-array-comparisons-in-powershell-with-a-runtime-regex.aspx\">this Hey, Scripting Guy! blog<\/a>.\nThe descriptions of the <b>eventids<\/b> and the sources being used were taken from the online documentation on the message tracking log fields, which can be found in the <a target=\"_blank\" href=\"http:\/\/technet.microsoft.com\/en-us\/library\/bb124375(EXCHG.80).aspx\">Exchange Server TechCenter Library<\/a>.\nFor the purposes of this exercise, we are will determine how much email is going in and out of our mail stores. This type of information is useful in capacity planning. The two types of events we are going to use are the <b>Receive<\/b> and <b>Deliver<\/b> events that have a Source field of STOREDRIVER.\nThe description of the <b>Deliver<\/b> events is pretty straightforward, and is shown here.\nA message was delivered to a mailbox.\nOn the other hand, <b>Receive<\/b> events are a little more complicated. Here is the description field from a receive event.\nA message was received and committed to the database. The RECEIVE event can be SMTP receive (Source: SMTP) or mail submitted by STOREDRIVER (Source: STOREDRIVER).\nSMTP RECEIVE can be from any source that submits a message by using SMTP. For example, it can be a Hub Transport server role, an Edge Transport server role, a non-Microsoft message transfer agent (MTA), or a POP\/IMAP client.\nThe Edge Transport process logs the STOREDRIVER RECEIVE event. This event directly corresponds to a STOREDRIVER SUBMIT event. The Mail Submission process logs the STOREDRIVER SUBMIT. These events can occur on the same server if both roles are installed locally, or they will be on different servers if the roles are split up due to load considerations and performance reasons.\n<b>Note&nbsp;&nbsp; <\/b>The process that handles the messages on Hub Transport and Edge Transport servers is referred to in the documentation as the Edge Transport process. It is essentially the same process running on both. It was first written for and used on Edge Transport servers; so on the Hub Transport server, the process inherited the name &ldquo;Edge Transport process.&rdquo;\nHere are a set of selected properties from the events types from which the script captures information. This set of events shows the events that are involved in the creation of a single email message, its submission and delivery, and the delivery of a subsequent reply.\n<b>Note<\/b>&nbsp; There are many more fields in the record. Including them all here would make the blog extremely long and rather tedious. However, there is much more information to be had from them, and I urge you to investigate a few of the many thousands you probably have in your very own tracking logs to find the ones you want to focus on in your environment.\nFollowing, is a STOREDRIVER RECEIVE event. A user whose mailbox is on server EXCH-MBX1.MYDOMAIN.COM generated this event. The user created and sent an email. The message has three recipients. One is a single internal recipient, one is a distribution group, and one is an external recipient. <b><\/b><\/p>\n<blockquote><p>  <span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">Source : STOREDRIVER<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">EventId : RECEIVE<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">MessageId : &lt;68ABA2FDBD44284790D6478BD77800CC1B6FDC7D@EXCH-MBX1.MYDOMAIN.COM&gt;<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">Recipients : {my_dl@mydomain.com, spooty@mydomain2.com, pinkie@brain.com}<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">TotalBytes : 12433<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">RecipientCount : 3<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">Sender : stewie@mydomain3.com<\/span><\/p>\n<\/blockquote>\n<p>In the following event, we have an EXPAND event message. You get one of these events when the server expands a distribution list (DL) into its constituent member addresses. The <b>RelatedRecipientAddress<\/b> is the DL that was expanded, and the <b>Recipients<\/b> are the members that it expanded to. We capture the addresses of the DLs from these. With this information we can we can report on which DLs are used and which ones are not utilized. The text of the EXPAND event message is shown here.<\/p>\n<blockquote><p>  <span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">Source : ROUTING<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">EventId : EXPAND<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">MessageId : &lt;68ABA2FDBD44284790D6478BD77800CC1B6FDC7D@EXCH-MBX1.MYDOMAIN.COM&gt;<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">Recipients : {yakko@mydomain2.com, wakko@mydomain2.com, dot@mydomain2.com}<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">RelatedRecipientAddress : my_dl@mydomain.com<\/span><\/p>\n<\/blockquote>\n<p>In the following message, we have our first DELIVER event message. This particular message is for the single, non-DL recipient. The DL to expand is split off, and the transport process will deliver any emails that have been resolved to their primary address and are ready for delivery. This happens even for strictly internal emails that happen to be all on the same mailbox server. One RECEIVE event can result in multiple DELIVER events.<\/p>\n<blockquote><p>  <span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">Source : STOREDRIVER<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">EventId : DELIVER<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">MessageId : &lt;68ABA2FDBD44284790D6478BD77800CC1B6FDC7D@EXCH-MBX1.MYDOMAIN.COM&gt;<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">Recipients : {spooty@mydomain2.com }<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">TotalBytes : 13236<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">RecipientCount : 1<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">Sender : stewie@mydomain3.com<\/span><\/p>\n<\/blockquote>\n<p>Now that the rest of the recipients have been resolved, the messages are ready to be delivered to the mailboxes. The DELIVER message from the STOREDRIVER source is shown here.<\/p>\n<blockquote><p>  <span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">Source : STOREDRIVER<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">EventId : DELIVER<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">MessageId : &lt;68ABA2FDBD44284790D6478BD77800CC1B6FDC7D@EXCH-MBX1.MYDOMAIN.COM&gt;<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">Recipients : {{yakko@mydomain2.com, wakko@mydomain2.com, dot@mydomain2.com}<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">TotalBytes : 13236<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">RecipientCount : 3<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">Sender : stewie@mydomain3.com<\/span><\/p>\n<\/blockquote>\n<p>A bit later, we get a reply from our external recipient. Note here that this message has a different message ID than the rest of the messages. It also has a source name of a mail server in a foreign domain. This can make things a bit confusing at first.<\/p>\n<blockquote><p>  <span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">Source : STOREDRIVER<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">EventId : DELIVER<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">MessageId : &lt;AANLkTimqe=wdGo4LQHQYWOxZqRfu2BHQAwz2kegEfRLb@mail.brain.com&gt;<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">Recipients : {stewie@mydomain3.com }<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">TotalBytes : 7052<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">RecipientCount : 1<\/span><\/p>\n<p><span style=\"font-family: Lucida Sans Typewriter;font-size: x-small\">Sender : pinkie@brain.com<\/span><\/p>\n<\/blockquote>\n<p>An <b>eventid<\/b> of DELIVER with a source of STOREDRIVER is a delivery of email to a mailbox server message.\nThe sender can be internal or external. Because it is being delivered to a mailbox server, we know all the recipients are internal. All of the received message stats can be extracted from the <b>Recipients<\/b> field of these events. DL expansion has already been done, so we can also capture the total number of internal emails and bytes for the senders here.\nFor sender processing, we start by determining if this is an internal email by checking the domain of the sender and the source of the <b>messageid<\/b>. If the sender is in one of our accepted domains, and the <b>messageid<\/b> source matches one of the mailbox servers, we consider it internal. We add the number of recipients to the total internal emails sent by the sender, and we add the number of recipients times the size of the email to his total internal bytes sent.\nIf it does not pass the tests for being an internal email, we consider it to be external, and we do not use it when accumulating sender statistics. You could add additional counters and logic here, and accumulate statistics on emails from external senders, by sender address.\nIn either case, we are going to add recipients to our address list, and increment the total number of internal messages received for each recipient, and then add the size of the email to their total internal bytes received. This portion of the Windows PowerShell script is shown here.<\/p>\n<p class=\"CodeBlockScreened\" style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: white\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: x-small\">if ($_.eventid -eq &#8220;DELIVER&#8221; -and $_.source -eq &#8220;STOREDRIVER&#8221;){<\/span><\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: white\"><span><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: x-small\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: white\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span style=\"font-size: x-small\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>if ($_.messageid -match &#8220;^.+@.+..+..+$&#8221; `<\/span><\/span><\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: white\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span style=\"font-size: x-small\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&nbsp;&nbsp; <\/span>-and $mbx_servers -contains $_.messageid.split(&#8220;@&#8221;)[1].trim(&#8220;&gt;&#8221;)`<\/span><\/span><\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: white\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span style=\"font-size: x-small\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&nbsp;&nbsp; <\/span>-and $accepted_domains -contains $_.sender.split(&#8220;@&#8221;)[1]) {<\/span><\/span><\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: white\"><span><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: x-small\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: white\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span style=\"font-size: x-small\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>$total_msgsent[$_.sender] += $_.recipientcount<\/span><\/span><\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: white\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span><span style=\"color: #000000;font-size: x-small\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"color: #000000;font-size: x-small\">$total_bytessent[$_.sender] += ($_.recipientcount * <br \/><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>$_.totalbytes)<\/span><\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: white\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span style=\"font-size: x-small\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>$total_msgsent_exch[$_.sender] += $_.recipientcount<\/span><\/span><\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: white\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span><span style=\"color: #000000;font-size: x-small\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"color: #000000;font-size: x-small\">$total_bytessent_exch[$_.sender] += ($_.totalbytes * <br \/><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>$_.recipientcount)<\/span><\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: white\"><span><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: x-small\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: white\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span style=\"font-size: x-small\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>foreach ($rcpt in $_.recipients){<\/span><\/span><\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: white\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span style=\"font-size: x-small\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>$exch_addrs[$rcpt] ++<\/span><\/span><\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: white\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span style=\"font-size: x-small\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>$msgrec[$rcpt] ++<\/span><\/span><\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: white\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span style=\"font-size: x-small\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>$bytesrec[$rcpt] += $_.totalbytes<\/span><\/span><\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: white\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span style=\"font-size: x-small\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>$msgrec_exch[$rcpt] ++<\/span><\/span><\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: white\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span style=\"font-size: x-small\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>$bytesrec_exch[$rcpt] += $_.totalbytes<\/span><\/span><\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: white\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span style=\"font-size: x-small\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>}<\/span><\/span><\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: white\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span style=\"font-size: x-small\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>}<\/span><\/span><\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: white\"><span><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: x-small\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: white\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span style=\"font-size: x-small\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>else {<\/span><\/span><\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: white\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span style=\"font-size: x-small\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>foreach ($rcpt in $_.recipients){<\/span><\/span><\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: white\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span style=\"font-size: x-small\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>$msgrec[$rcpt] ++<\/span><\/span><\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: white\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span style=\"font-size: x-small\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>$bytesrec[$rcpt] += $_.totalbytes<\/span><\/span><\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: white\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span style=\"font-size: x-small\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>$msgrec_smtpext[$rcpt] ++<\/span><\/span><\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: white\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span style=\"font-size: x-small\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>$bytesrec_smtpext[$rcpt] += $_.totalbytes<\/span><\/span><\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: white\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span style=\"font-size: x-small\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>}<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: white\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span style=\"font-size: x-small\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>}<\/span><\/span><\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: white\"><span><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: x-small\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: white\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span style=\"font-size: x-small\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>}<\/span><\/span><\/span><\/span>\nAn <b>eventid<\/b> of RECEIVE with a source of STOREDRIVER represents a message that is being submitted by a mailbox server for delivery.\nThe sender will be an internal user because it came from a mailbox server. The recipients may be internal, external, or both. The message might also be addressed to distribution lists. That means that we do not really know how many final recipients there will be or who the actual recipients are, so we will not be collecting any recipient statistics from these events.\nThe events with an <b>eventid<\/b> of RECEIVE and a source of STOREDRIVER represent a user who is composing and sending a unique email, so we will gather the counts of unique emails sent internally and externally from here. By comparing the domain part of the recipient&#8217;s address to the list of accepted domains, the script will determine whether to increment the internal counters, external counters, or both counters for unique messages and bytes sent.\nWe will also accumulate the total external messages and bytes sent in this portion of the script. Because addresses are external, they will not show up in the DELIVER or STOREDRIVER events (as previously discussed). This portion of the script is shown here.<\/p>\n<p class=\"CodeBlockScreened\" style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: white\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: x-small\">if ($_.eventid -eq &#8220;RECEIVE&#8221; -and $_.source -eq &#8220;STOREDRIVER&#8221;){<\/span><\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: white\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span style=\"font-size: x-small\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>$exch_addrs[$_.sender] ++<\/span><\/span><\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: white\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span style=\"font-size: x-small\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>$unique_msgsent[$_.sender] ++<\/span><\/span><\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: white\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span style=\"font-size: x-small\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>$unique_bytessent[$_.sender] += $_.totalbytes<\/span><\/span><\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: white\"><span><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: x-small\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: white\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span style=\"font-size: x-small\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>$recpt_domains = $_.recipients |% {$_.split(&#8220;@&#8221;)[1]}<span>&nbsp;&nbsp; <\/span><\/span><\/span><\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: white\"><span><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: x-small\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: white\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span style=\"font-size: x-small\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>if ($recpt_domains |? {$accepted_domains -contains $_}){<\/span><\/span><\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"line-height: 10pt;list-style-type: disc;margin: 15pt 17.3pt 0.25in 0in;background: white\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span style=\"font-size: x-small\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><\/span><\/span><\/span><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to use Windows PowerShell to evaluate email messages that are sent and received on Microsoft Exchange Server. Hey, Scripting Guy! I need to be able to create a report that tells my boss information about email messages that are sent and received by our Exchange Server. Can I use Windows PowerShell to [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[56,28,180,223,3,45],"class_list":["post-15441","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-guest-blogger","tag-messaging-and-communication","tag-microsoft-exchange-2010","tag-rob-campbell","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to use Windows PowerShell to evaluate email messages that are sent and received on Microsoft Exchange Server. Hey, Scripting Guy! I need to be able to create a report that tells my boss information about email messages that are sent and received by our Exchange Server. Can I use Windows PowerShell to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/15441","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=15441"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/15441\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=15441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=15441"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=15441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}