Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- .Title - AutoAccess Script
- .DESCRIPTION
- . 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",
- If successful, they will receive an email letting them know it has been done
- if it fails they will be told that this has failed
- the email that is received should then be moved to the deleted items folder
- .Usage
- untested, but you should be able to adjust the variables, and run it as a scheduled task
- .resources
- This was derived from articles on technet
- 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
- Glens Exchange Dev Blog - http://gsexdev.blogspot.co.uk/
- 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/
- #>
- ##import-module, set variables for basic use of EWS with your own mailbox
- ##can get EWS from MSDN - http://http://msdn.microsoft.com/en-us/library/dd633709
- Import-Module -Name "C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll"
- $mailbox="mailbox@domain.com"
- ##if you're using Exchange 2010 SP1 you don't need to worry about putting in the version
- $exchservice= New-Object Microsoft.Exchange.WebServices.Data.ExchangeService exchange2010
- ##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)
- $exchservice.UseDefaultCredentials=$true
- #$creds = New-Object System.Net.NetworkCredential("username","password" )
- #$exchservice.Credentials = $creds
- #$exchservice.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, $mailbox)
- #get the EWS settings from autodiscover
- $exchservice.autodiscoverurl($mailbox)
- ##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
- $admincreds=Get-Credential domain\adminaccount
- ##import pssession from exchangeserver
- $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "http://exchangeserver.domain.local/powershell/" -cred $admincreds
- Import-PSSession $Session
- ##set variables for $inbox
- ##Bind $inbox to the folder known as inbox
- ##bind $deleteditems to the folder known as "deleted items"
- $inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($exchservice,[Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::inbox)
- $deleteditems = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($exchservice,[Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::deleteditems)
- #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
- $ivItemView = New-Object Microsoft.Exchange.WebServices.Data.ItemView(100)
- ##I have no idea why $fiitems is set as $null and then set as something else - I should probably look this up
- $fiItems = $null
- $psPropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::full)
- $fiItems = $exchservice.FindItems($inbox.Id,$ivItemView)
- $exchservice.LoadPropertiesForItems::$fiItems,$psPropset
- foreach($Item in $fiItems.Items){
- ##because want to send an email using EWS later - couldn't find the SMTP address in the $item object
- $sender=get-mailbox $item.lastmodifiedname
- $ccd=get-mailbox $item.displaycc
- if ($item.subject -like "autoaccess") {
- try {
- Add-mailboxpermission -identity $sender.SamAccountName -user $ccd.SamAccountName -accessrights fullaccess
- #Create a new email message object
- $mail = New-Object Microsoft.Exchange.WebServices.Data.EmailMessage($exchservice)
- #Set the subject and body based on function parameters
- $mail.Subject = "Mail to AutoAccess Bot - Success"
- $mail.Body = "AutoAccessBot has granted proxy access to your ($sender) mailbox for ($ccd)"
- $mail.ToRecipients.Add($sender.primarysmtpaddress)
- #Send the message
- $mail.Send()}
- catch {#Create a new email message object
- $mail = New-Object Microsoft.Exchange.WebServices.Data.EmailMessage($exchservice)
- #Set the subject and body based on function parameters
- $mail.Subject = "Mail to AutoAccess Bot - Failure"
- $mail.Body = "AutoAccessBot has Not granted access to your ($sender) mailbox for the person you CC'd in ($ccd)"
- $mail.ToRecipients.Add($sender.primarysmtpaddress)
- #Send the message
- $mail.Sendandsavecopy()}
- }
- elseif ($item.subject -like "sendonbehalfof") {
- try {
- set-mailbox -identity $sender.SamAccountName -grantsendonbehalfto $ccd.SamAccountName
- #Create a new email message object
- $mail = New-Object Microsoft.Exchange.WebServices.Data.EmailMessage($exchservice)
- #Set the subject and body based on function parameters
- $mail.Subject = "Mail to AutoAccess Bot - Success"
- $mail.Body = "AutoAccessBot has granted send on behalf of privelidges to your ($sender) mailbox for ($ccd)"
- $mail.ToRecipients.Add($sender.primarysmtpaddress)
- #Send the message
- $mail.Send()}
- catch {#Create a new email message object
- $mail = New-Object Microsoft.Exchange.WebServices.Data.EmailMessage($exchservice)
- #Set the subject and body based on function parameters
- $mail.Subject = "Mail to AutoAccess Bot - Failure"
- $mail.Body = "AutoAccessBot has Not granted send on behalf of privelidges to your ($sender) mailbox for ($ccd)"
- $mail.ToRecipients.Add($sender.primarysmtpaddress)
- #Send the message
- $mail.Sendandsavecopy()}
- }
- else {#Create a new email message object
- $mail = New-Object Microsoft.Exchange.WebServices.Data.EmailMessage($exchservice)
- #Set the subject and body based on function parameters
- $mail.Subject = "Mail to AutoAccess Bot - Failure"
- $mail.Body = "AutoAccessBot has Not granted access to your ($sender) mailbox for the person you CC'd in ($ccd)"
- $mail.ToRecipients.Add($sender.primarysmtpaddress)
- #Send the message
- $mail.Sendandsavecopy()}
- $item.move($deleteditems.id)}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement