Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Load the ActiveDirector PowerShell module
- Import-Module ActiveDirectory
- # Load the EWS Managed API library
- Add-Type -Path "$env:ProgramFiles\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"
- Try {
- #Define the Exchange server version
- #Had to use SP1 even though we have SP3, kept getting BIND errors when defining $inbox
- $Exchange2010SP1 = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP1
- # create EWS Service object for the target mailbox name
- $EWS = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService -ArgumentList $Exchange2010SP1
- $EWS.AutodiscoverUrl("Employee.Terminations@mycompany.com")
- #Setup variable for the Folder Root
- $MailboxRootid = New-Object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::MsgFolderRoot)
- $MailboxRoot = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($EWS, $MailboxRootid)
- $FolderList = New-Object Microsoft.Exchange.WebServices.Data.FolderView(20)
- $FolderList.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep
- $FolderResults = $MailboxRoot.FindFolders($FolderList)
- #Setup variable for the Inbox and number of items to get from the Inbox
- $InboxID = New-Object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox)
- $Inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($ews, $InboxID)
- $mailList = $Inbox.FindItems(50)
- #Do the work
- #Iterate through each email in the Inbox and search for a employee name
- foreach ($email in $mailList) {
- $email.Load()
- $StringToSearch = $email.Subject
- #RegEx to search the Subject line for a username
- $name = [regex]::match($StringToSearch, 'Notification\:([^\)]+)\(').Groups[1].Value
- #Trim leading and trailing spaces from the username
- $name = $name.TrimStart([char]0x0020).TrimEnd([char]0x0020)
- #Check if there is a user account that matches the employee name
- #If one exists, send the notification to the Help Desk and save a copy
- if ((Get-ADUser -Filter {Name -like $name}) -ne $null) {
- $fwMail = New-Object -TypeName Microsoft.Exchange.WebServices.Data.EmailMessage -ArgumentList $EWS
- $fwMail.Subject = $email.Subject
- $fwMail.Body = $email.Body
- $fwMail.ToRecipients.Add('Help.Desk@mycompany.com') | Out-Null
- $fwMail.SendAndSaveCopy()
- }
- #Mark the email as read and move the processed email to the Processed folder.
- $email.IsRead = $true
- $email.Update([Microsoft.Exchange.WebServices.Data.ConflictResolutionMode]::AutoResolve)
- $email.move(($FolderResults | Where-Object {$_.displayname -eq "Processed"}).ID) | Out-Null
- }
- }
- Catch {
- $ErrorMessage = $_.Exception.Message
- $FailedItem = $_.Exception.ItemName
- 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"
- Break
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement