Forum Discussion
Need an example PowerShell script to get attachments from an O365 Exchange Online Shared Mailbox
I need an example PowerShell script to get attachments from an O365 Exchange Online Shared Mailbox. Or even a regular user mailbox.
Anyone got one available or know where I can find one?
You have two options here: use some EWS based script (Glen's blog is usually the best resource for anything EWS related: http://gsexdev.blogspot.bg/2010/01/writing-simple-scripted-process-to.html) or use the Outlook COM object model if you have a single shared mailbox that you have configured in Outlook (example here https://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?forum=exchange2010)
- Brent EllisSilver Contributor
Thanks, I ended up formulating the solution based on a solution from @Eli Van Eenwyk
https://gallery.technet.microsoft.com/office/O365-Email-Attachments-to-6a45e84c
$cred = Get-Credential $sharedMailbox = "shared@contoso.onmicrosoft.com" $url = "https://outlook.office365.com/api/v1.0/users/$sharedMailbox/messages" $date = "2016-08-03" ## Get all messages that have attachments where received date is greater than $date $messageQuery = $url + "?`$select=Id&`$filter=HasAttachments eq true and DateTimeReceived ge " + $date $messages = Invoke-RestMethod $messageQuery -Credential $cred ## Loop through each results foreach ($message in $messages.value){ # get attachments and save to file system $query = $url + "/" + $message.Id + "/attachments" $attachments = Invoke-RestMethod $query -Credential $cred # in case of multiple attachments in email foreach ($attachment in $attachments.value){ $attachment.Name $path = "c:\Temp\" + $attachment.Name $Content = [System.Convert]::FromBase64String($attachment.ContentBytes) Set-Content -Path $path -Value $Content -Encoding Byte } # Move processed email to a subfolder $query = $url + "/" + $message.Id + "/move" $body="{""DestinationId"":""AAMkAGRiZmVmOTFlLWJmNjctNDVmZi1iZDkyLTZhOTEzZjI4MGJkNQAuAAAAAAAAkEHub27VS7X8pWwWnKIcAQCxICvUWGkmS6kBXjFB5cP/AADk/q7pAAA=""}" Invoke-RestMethod $query -Body $body -ContentType "application/json" -Method post -Credential $cred }
Oh right, we also have REST APIs now :) Gotta start learning those...