Advertisement
infinit_e

EWS Mail Sort

Jun 22nd, 2017
1,004
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Load the ActiveDirector PowerShell module
  2. Import-Module ActiveDirectory
  3. # Load the EWS Managed API library
  4. Add-Type -Path "$env:ProgramFiles\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"
  5.  
  6. Try {
  7.     #Define the Exchange server version
  8.     #Had to use SP1 even though we have SP3, kept getting BIND errors when defining $inbox
  9.     $Exchange2010SP1 = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP1
  10.  
  11.     # create EWS Service object for the target mailbox name
  12.     $EWS = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService -ArgumentList $Exchange2010SP1
  13.     $EWS.AutodiscoverUrl("Employee.Terminations@mycompany.com")
  14.  
  15.     #Setup variable for the Folder Root
  16.     $MailboxRootid = New-Object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::MsgFolderRoot)
  17.     $MailboxRoot = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($EWS, $MailboxRootid)
  18.  
  19.     $FolderList = New-Object Microsoft.Exchange.WebServices.Data.FolderView(20)
  20.     $FolderList.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep
  21.     $FolderResults = $MailboxRoot.FindFolders($FolderList)
  22.  
  23.     #Setup variable for the Inbox and number of items to get from the Inbox
  24.     $InboxID = New-Object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox)
  25.     $Inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($ews, $InboxID)
  26.     $mailList = $Inbox.FindItems(50)
  27.  
  28.     #Do the work
  29.     #Iterate through each email in the Inbox and search for a employee name
  30.     foreach ($email in $mailList) {
  31.         $email.Load()
  32.         $StringToSearch = $email.Subject
  33.         #RegEx to search the Subject line for a username
  34.         $name = [regex]::match($StringToSearch, 'Notification\:([^\)]+)\(').Groups[1].Value
  35.         #Trim leading and trailing spaces from the username
  36.         $name = $name.TrimStart([char]0x0020).TrimEnd([char]0x0020)
  37.    
  38.         #Check if there is a user account that matches the employee name
  39.         #If one exists, send the notification to the Help Desk and save a copy
  40.         if ((Get-ADUser -Filter {Name -like $name}) -ne $null) {
  41.             $fwMail = New-Object -TypeName Microsoft.Exchange.WebServices.Data.EmailMessage -ArgumentList $EWS
  42.             $fwMail.Subject = $email.Subject
  43.             $fwMail.Body = $email.Body
  44.             $fwMail.ToRecipients.Add('Help.Desk@mycompany.com') | Out-Null
  45.             $fwMail.SendAndSaveCopy()
  46.         }
  47.  
  48.         #Mark the email as read and move the processed email to the Processed folder.
  49.         $email.IsRead = $true
  50.         $email.Update([Microsoft.Exchange.WebServices.Data.ConflictResolutionMode]::AutoResolve)
  51.         $email.move(($FolderResults | Where-Object {$_.displayname -eq "Processed"}).ID) | Out-Null
  52.  
  53.     }
  54. }
  55.  
  56. Catch {
  57.     $ErrorMessage = $_.Exception.Message
  58.     $FailedItem = $_.Exception.ItemName
  59.     Send-MailMessage -From Employee.Terminations@mycompany.com -To Help.Desk@mycompany.com -Subject "Termination Notification processing error" -SmtpServer smtp.mycompany.com -Body "$FailedItem. The error message was $ErrorMessage"
  60.     Break
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement