Advertisement
ch13fwiggum

AutoAccessBot

Nov 6th, 2013
488
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. .Title - AutoAccess Script
  3.  
  4.    
  5. .DESCRIPTION
  6.     . This script will retrieve the emails in a mailbox $mailbox and grant fullaccess to the sender's mailbox to the person they CC'd in, as long as they use the subject "autoaccess",
  7.    
  8.    If successful, they will receive an email letting them know it has been done
  9.    if it fails they will be told that this has failed
  10.    the email that is received should then be moved to the deleted items folder
  11.    
  12.      
  13. .Usage
  14.      untested, but you should be able to adjust the variables, and run it as a scheduled task
  15.  
  16. .resources
  17.    This was derived from articles on technet
  18.    http://social.technet.microsoft.com/Forums/exchange/en-US/c4d4cca8-2bc0-48a9-97f8-61fc68c8079c/powershell-script-to-get-all-new-attachments-and-store-them-on-a-network-share
  19.    Glens Exchange Dev Blog - http://gsexdev.blogspot.co.uk/
  20.    and Mike Pfeiffer's Script CMDlets for EWS - http://www.mikepfeiffer.net/2011/04/powershell-script-cmdlets-for-managing-e-mail-items-using-the-exchange-web-services-managed-api/
  21. #>
  22.  
  23. ##import-module, set variables for basic use of EWS with your own mailbox
  24. ##can get EWS from MSDN - http://http://msdn.microsoft.com/en-us/library/dd633709
  25. Import-Module -Name "C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll"
  26. $mailbox="mailbox@domain.com"
  27. ##if you're using Exchange 2010 SP1 you don't need to worry about putting in the version
  28. $exchservice= New-Object Microsoft.Exchange.WebServices.Data.ExchangeService exchange2010
  29.  
  30. ##set the credentials for $mailbox, either use default or use another (if you need to impersonate a different user see http://msdn.microsoft.com/en-us/library/dd633680)
  31. $exchservice.UseDefaultCredentials=$true
  32. #$creds = New-Object System.Net.NetworkCredential("username","password" )
  33. #$exchservice.Credentials = $creds
  34. #$exchservice.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, $mailbox)
  35. #get the EWS settings from autodiscover
  36. $exchservice.autodiscoverurl($mailbox)
  37.  
  38. ##set $admincreds for the account that will make changes - probably not needed if you create an admin-enabled mailbox and log in as the user
  39. $admincreds=Get-Credential domain\adminaccount
  40. ##import pssession from exchangeserver
  41. $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "http://exchangeserver.domain.local/powershell/" -cred $admincreds
  42. Import-PSSession $Session
  43. ##set variables for $inbox
  44. ##Bind $inbox to the folder known as inbox
  45. ##bind $deleteditems to the folder known as "deleted items"
  46. $inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($exchservice,[Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::inbox)
  47. $deleteditems = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($exchservice,[Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::deleteditems)
  48. #Define $ItemView to retrive 100 Items at a time and set property - this was just so I could have a look at what i could return from $item
  49. $ivItemView = New-Object Microsoft.Exchange.WebServices.Data.ItemView(100)
  50. ##I have no idea why $fiitems is set as $null and then set as something else - I should probably look this up
  51. $fiItems = $null
  52. $psPropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::full)
  53. $fiItems = $exchservice.FindItems($inbox.Id,$ivItemView)
  54. $exchservice.LoadPropertiesForItems::$fiItems,$psPropset
  55. foreach($Item in $fiItems.Items){
  56. ##because want to send an email using EWS later - couldn't find the SMTP address in the $item object
  57. $sender=get-mailbox $item.lastmodifiedname
  58. $ccd=get-mailbox $item.displaycc
  59.  
  60. if ($item.subject -like "autoaccess") {
  61. try {
  62. Add-mailboxpermission -identity $sender.SamAccountName -user $ccd.SamAccountName -accessrights fullaccess
  63.         #Create a new email message object
  64.         $mail = New-Object Microsoft.Exchange.WebServices.Data.EmailMessage($exchservice)
  65.         #Set the subject and body based on function parameters
  66.         $mail.Subject = "Mail to AutoAccess Bot - Success"
  67.         $mail.Body = "AutoAccessBot has granted proxy access to your ($sender) mailbox for ($ccd)"
  68.         $mail.ToRecipients.Add($sender.primarysmtpaddress) 
  69.         #Send the message
  70.         $mail.Send()}
  71. catch {#Create a new email message object
  72.         $mail = New-Object Microsoft.Exchange.WebServices.Data.EmailMessage($exchservice)
  73.         #Set the subject and body based on function parameters
  74.         $mail.Subject = "Mail to AutoAccess Bot - Failure"
  75.         $mail.Body = "AutoAccessBot has Not granted access to your ($sender) mailbox for the person you CC'd in ($ccd)"
  76.         $mail.ToRecipients.Add($sender.primarysmtpaddress)
  77.         #Send the message
  78.         $mail.Sendandsavecopy()}
  79.  
  80. }
  81. elseif ($item.subject -like "sendonbehalfof") {
  82. try {
  83. set-mailbox -identity $sender.SamAccountName -grantsendonbehalfto $ccd.SamAccountName
  84.         #Create a new email message object
  85.         $mail = New-Object Microsoft.Exchange.WebServices.Data.EmailMessage($exchservice)
  86.         #Set the subject and body based on function parameters
  87.         $mail.Subject = "Mail to AutoAccess Bot - Success"
  88.         $mail.Body = "AutoAccessBot has granted send on behalf of privelidges to your ($sender) mailbox for ($ccd)"
  89.         $mail.ToRecipients.Add($sender.primarysmtpaddress) 
  90.         #Send the message
  91.         $mail.Send()}
  92. catch {#Create a new email message object
  93.         $mail = New-Object Microsoft.Exchange.WebServices.Data.EmailMessage($exchservice)
  94.         #Set the subject and body based on function parameters
  95.         $mail.Subject = "Mail to AutoAccess Bot - Failure"
  96.         $mail.Body = "AutoAccessBot has Not granted send on behalf of privelidges to your ($sender) mailbox for ($ccd)"
  97.         $mail.ToRecipients.Add($sender.primarysmtpaddress)
  98.         #Send the message
  99.         $mail.Sendandsavecopy()}
  100.  
  101. }
  102. else {#Create a new email message object
  103.         $mail = New-Object Microsoft.Exchange.WebServices.Data.EmailMessage($exchservice)
  104.         #Set the subject and body based on function parameters
  105.         $mail.Subject = "Mail to AutoAccess Bot - Failure"
  106.         $mail.Body = "AutoAccessBot has Not granted access to your ($sender) mailbox for the person you CC'd in ($ccd)"
  107.         $mail.ToRecipients.Add($sender.primarysmtpaddress)
  108.         #Send the message
  109.         $mail.Sendandsavecopy()}
  110.  
  111.  
  112. $item.move($deleteditems.id)}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement