Need an example PowerShell script to get attachments from an O365 Exchange Online Shared Mailbox

Silver Contributor

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?

6 Replies

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/powe...)

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...

What is your opinion of the API's, and graph as a whole. I like the dev talks i go to about it, but have struggled to find a use for them outside of just doing my powershell scripts. Granted I do not application development, but ya.... curious as to your thought Vasil.

I am getting AUTH errors.  I am using my Global Admin account and my account for the inbox I am trying to extract attachment from.

 

My use case is a bit different.  I need to automate a barrage of emails we received from our FirePower device.  In short, the number of emails/attachments are too numerous to process manually.

 

I would like to save each attachment from a senderaddress to a folder on my computer.  

 

I am thinking that I need to create a Graph API.  

@QuentinLMcCallum-AWA Power Automate (former Flow) has templates for this and can be done within seconds if that helps.