# Tracking E-mail on Exchange With Powershell

Contents

I had a problem with the delivery of e-mails. The script presented below creates an Excel document with all the e-mails sent from a specific e-mail address with the event. The script takes four arguments:

2. StartDate
3. EndDate
4. Output

The address list is a mandatory parameter containing the full path to the text file containing the e-mail addresses. The file is formatted as follows:

Email
[email protected]
[email protected]

StartDate

The start date is the date from when the list is generated. The start date is not mandatory. If no parameter is given the standard start date is seven days from the day the script is executed.

EndDate

The end date is the date at which the list ends. The end date is not mandatory. If no parameter is given the standard end date is the day the script is executed.

Output

This parameter is the full path to a directory where the files need to be saved. The parameter is not mandatory. If no parameter is given the script will open a new Excel document for each e-mail address present in the source file. The script outputs an Excel document containing e-mail information with the columns: Timestamp, Sender, Receiver, and Event.

Usage

Without the start date and end date

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 # PowerShell script to get message tracking of an e-mail address # # Author: Sander Stad # Version: 1.1 January 2011 tested on Powershell v2.0 # # Changes: # Version 1.0: # Initial version worked as it should # Version 1.1: # Implementation of parameters # Implementation of constants # Error check on parameters # Implemented output to Excell file on disk # # Usage: # .\\Get-MessageTracking.ps1 -AddressList 'c:\\addresses.txt' -Output 'c:\\output' Param( [Parameter(position=1,Mandatory = $true )\]$string$$AddressList, # List of servers that needs to be checked. Has to be a text file! [Parameter(position=2,Mandatory = $false )\]$string$$StartDate, # Start date for the querie [Parameter(position=2,Mandatory = $false )\]$string$$EndDate, # End date for the querie [Parameter(position=2,Mandatory = $false )\]$string$$Output # Output directory for the Excell file. If no value is given an Excell sheet will open. ) Process { # Set constant variables Set-Variable -Name _DATESTART -Option Constant -Value (Get-Date).Adddays(-7) Set-Variable -Name _DATEEND -Option Constant -Value (Get-Date) Set-Variable -Name _WAITTIME -Option Constant -Value 2 CLS # Set global error check # Value 1: High error program will stop # Value 2: Warning program will continue # Value 3: Information about execution $ErrorStatus = 0; #Set Excel activity # Value 1: Excell sheet will open on execute (Default value) # Value 2: Excell sheet will be written to the given path$ExcelAction = 0; If($Output -eq '') { Write-Host "INFO: Path for output not given. Excell sheet will be opened." -ForegroundColor Red -BackgroundColor Green$ExcelAction = 1 $ErrorStatus = 3 } ElseIf(Test-Path$Output) { Write-Host "INFO: Excell sheet will be opened written to $Output." -ForegroundColor Red -BackgroundColor Green$ExcelAction = 2 $ErrorStatus = 3 } Else { Write-Host "INFO: Path for output is set. Data will be written to$Output." -ForegroundColor Red -BackgroundColor Green $ExcelAction = 2$ErrorStatus = 3 } # Check all the parameters If($AddressList -eq '') { Write-Host "ERROR: Server list not given" -ForegroundColor Red -BackgroundColor Yellow$ErrorStatus = 1 } ElseIf(!(Test-Path $AddressList)) { Write-Host "ERROR: Path to server list doesn't exist" -ForegroundColor Red -BackgroundColor Yellow$ErrorStatus = 1 } If($StartDate -eq '') {$dateStart = (Get-Date).Adddays(-7) Write-Host "INFO: Start date is not given. Start date is set to: $dateStart." -ForegroundColor Red -BackgroundColor Green$ExcelAction = 2 $ErrorStatus = 3 } Else {$dateStart = $StartDate } If($EndDate -eq '') { $dateEnd = (Get-Date) Write-Host "INFO: End date is not given. End date is set to:$dateEnd." -ForegroundColor Red -BackgroundColor Green $ExcelAction = 2$ErrorStatus = 3 } Else { $dateEnd =$EndDate } # Check if any error occurred in the parameters If($ErrorStatus -eq 0) { Write-Host "" Write-Host "No errors found in the parameters. Continuing....." -ForegroundColor Green Start-Sleep -Seconds$\_WAITTIME } ElseIf($ErrorStatus -eq 1) { Write-Host "" Write-Host "Critical errors found. Exiting..." -ForegroundColor Red Start-Sleep -Seconds$\_WAITTIME Exit } ElseIf($ErrorStatus -eq 2) { Write-Host "" Write-Host "Warnings found in the parameters. Continuing....." -ForegroundColor Blue Start-Sleep -Seconds$\_WAITTIME } ElseIf($ErrorStatus -eq 3) { Write-Host "" Write-Host "Informational messages found. Continuing....." -ForegroundColor Green Start-Sleep -Seconds$\_WAITTIME } #Get all the addresses $addresses = Import-Csv$AddressList #Read through the contents of the source file Foreach($address In$addresses) { # Set the settings for the Excel sheet If($ExcelAction -eq 1) { #Create a new Excel object using COM$Excel = New-Object -ComObject Excel.Application $Excel.visible =$True $Excel =$Excel.Workbooks.Add() $Sheet =$Excel.Worksheets.Item(1) } ElseIf($ExcelAction -eq 2) { #Create a new Excel object using COM$Excel = New-Object -ComObject Excel.Application $Excel.visible =$False #$Excel =$Excel.Workbooks.Open() $Excel =$Excel.Workbooks.Add() $Sheet =$Excel.Worksheets.Item(1) } #Counter variable for rows $intRow = 1$Sheet.Cells.Item($intRow,1) = "TIMESTAMP"$Sheet.Cells.Item($intRow,2) = "SENDER"$Sheet.Cells.Item($intRow,3) = "RECIPIENTS"$Sheet.Cells.Item($intRow,4) = "EVENT" #Format the column headers For ($col = 1; $col –le 4;$col++) { $Sheet.Cells.Item($intRow,$col).Font.Bold =$True $Sheet.Cells.Item($intRow,$col).Interior.ColorIndex = 48$Sheet.Cells.Item($intRow,$col).Font.ColorIndex = 34 } #Increase the row number $intRow++ #Get the e-mail address from the list$emailAddress = $address.Email #Get the results$results = Get-TransportServer | Get-MessageTrackingLog -Sender $emailAddress -Start$_DATESTART -End $\_DATEEND | Select-Object Timestamp, Sender, Recipients, EventId | sort Timestamp Foreach($result In $results) { #Write-Host$result.Timestamp ',' $result.Sender ','$result.Recipients ',' $result.EventId # Insert the values$Sheet.Cells.Item($intRow, 1) =$result.Timestamp $Sheet.Cells.Item($intRow, 2) = $result.Sender$Sheet.Cells.Item($intRow, 3) =$result.Recipients $Sheet.Cells.Item($intRow, 4) = $result.EventId # Increment the row number$intRow ++ } # Increment the row number $intRow ++ # Transform the email address for a valid file name$at = $emailAddress.IndexOf("@")$filename = $emailAddress.ToLower().Replace(".", "\_").Substring(0,$at) $destination =$Output + "\\$filename" # Automatically size the field in the excel sheet$Sheet.UsedRange.EntireColumn.AutoFit() # Close the document if it's automatically written to disk If($ExcelAction -eq 2) {$Excel.SaveAs($destination, 56)$Excel.Close(\$False) } } }