Home

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

%3CLINGO-SUB%20id%3D%22lingo-sub-4400%22%20slang%3D%22en-US%22%3ENeed%20an%20example%20PowerShell%20script%20to%20get%20attachments%20from%20an%20O365%20Exchange%20Online%20Shared%20Mailbox%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-4400%22%20slang%3D%22en-US%22%3E%3CP%3EI%20need%20an%20example%20PowerShell%20script%20to%20get%20attachments%20from%20an%20O365%20Exchange%20Online%20Shared%20Mailbox.%20%26nbsp%3BOr%20even%20a%20regular%20user%20mailbox.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAnyone%20got%20one%20available%20or%20know%20where%20I%20can%20find%20one%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-4400%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExchange%20Online%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-5102%22%20slang%3D%22en-US%22%3ERe%3A%20Need%20an%20example%20PowerShell%20script%20to%20get%20attachments%20from%20an%20O365%20Exchange%20Online%20Shared%20Mailbox%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-5102%22%20slang%3D%22en-US%22%3EWhat%20is%20your%20opinion%20of%20the%20API's%2C%20and%20graph%20as%20a%20whole.%20I%20like%20the%20dev%20talks%20i%20go%20to%20about%20it%2C%20but%20have%20struggled%20to%20find%20a%20use%20for%20them%20outside%20of%20just%20doing%20my%20powershell%20scripts.%20Granted%20I%20do%20not%20application%20development%2C%20but%20ya....%20curious%20as%20to%20your%20thought%20Vasil.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-4504%22%20slang%3D%22en-US%22%3ERe%3A%20Need%20an%20example%20PowerShell%20script%20to%20get%20attachments%20from%20an%20O365%20Exchange%20Online%20Shared%20Mailbox%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-4504%22%20slang%3D%22en-US%22%3E%3CP%3EOh%20right%2C%20we%20also%20have%20REST%20APIs%20now%20%3A)%3C%2Fimg%3E%20Gotta%20start%20learning%20those...%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-4470%22%20slang%3D%22en-US%22%3ERe%3A%20Need%20an%20example%20PowerShell%20script%20to%20get%20attachments%20from%20an%20O365%20Exchange%20Online%20Shared%20Mailbox%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-4470%22%20slang%3D%22en-US%22%3E%3CP%3EThanks%2C%20I%20ended%20up%20formulating%20the%20solution%20based%20on%20a%20solution%20from%26nbsp%3B%40Eli%20Van%20Eenwyk%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fgallery.technet.microsoft.com%2Foffice%2FO365-Email-Attachments-to-6a45e84c%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgallery.technet.microsoft.com%2Foffice%2FO365-Email-Attachments-to-6a45e84c%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%24cred%20%3D%20Get-Credential%0A%0A%24sharedMailbox%20%3D%20%22shared%40contoso.onmicrosoft.com%22%0A%24url%20%3D%20%22https%3A%2F%2Foutlook.office365.com%2Fapi%2Fv1.0%2Fusers%2F%24sharedMailbox%2Fmessages%22%0A%24date%20%3D%20%222016-08-03%22%0A%0A%23%23%20Get%20all%20messages%20that%20have%20attachments%20where%20received%20date%20is%20greater%20than%20%24date%20%0A%24messageQuery%20%3D%20%24url%20%2B%20%22%3F%60%24select%3DId%26amp%3B%60%24filter%3DHasAttachments%20eq%20true%20and%20DateTimeReceived%20ge%20%22%20%2B%20%24date%0A%24messages%20%3D%20Invoke-RestMethod%20%24messageQuery%20-Credential%20%24cred%0A%0A%23%23%20Loop%20through%20each%20results%0Aforeach%20(%24message%20in%20%24messages.value)%7B%0A%0A%20%20%20%20%23%20get%20attachments%20and%20save%20to%20file%20system%0A%20%20%20%20%24query%20%3D%20%24url%20%2B%20%22%2F%22%20%2B%20%24message.Id%20%2B%20%22%2Fattachments%22%0A%20%20%20%20%24attachments%20%3D%20Invoke-RestMethod%20%24query%20-Credential%20%24cred%0A%0A%20%20%20%20%23%20in%20case%20of%20multiple%20attachments%20in%20email%0A%20%20%20%20foreach%20(%24attachment%20in%20%24attachments.value)%7B%0A%20%20%20%20%20%20%20%20%24attachment.Name%0A%20%20%20%20%20%20%20%20%24path%20%3D%20%22c%3A%5CTemp%5C%22%20%2B%20%24attachment.Name%0A%20%20%20%20%0A%20%20%20%20%20%20%20%20%24Content%20%3D%20%5BSystem.Convert%5D%3A%3AFromBase64String(%24attachment.ContentBytes)%0A%20%20%20%20%20%20%20%20Set-Content%20-Path%20%24path%20-Value%20%24Content%20-Encoding%20Byte%0A%20%20%20%20%7D%0A%0A%20%20%20%20%23%20Move%20processed%20email%20to%20a%20subfolder%0A%20%20%20%20%24query%20%3D%20%24url%20%2B%20%22%2F%22%20%2B%20%24message.Id%20%2B%20%22%2Fmove%22%0A%20%20%20%20%24body%3D%22%7B%22%22DestinationId%22%22%3A%22%22AAMkAGRiZmVmOTFlLWJmNjctNDVmZi1iZDkyLTZhOTEzZjI4MGJkNQAuAAAAAAAAkEHub27VS7X8pWwWnKIcAQCxICvUWGkmS6kBXjFB5cP%2FAADk%2Fq7pAAA%3D%22%22%7D%22%0A%20%20%20%20Invoke-RestMethod%20%24query%20-Body%20%24body%20-ContentType%20%22application%2Fjson%22%20-Method%20post%20-Credential%20%24cred%0A%0A%7D%0A%0A%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-4413%22%20slang%3D%22en-US%22%3ERe%3A%20Need%20an%20example%20PowerShell%20script%20to%20get%20attachments%20from%20an%20O365%20Exchange%20Online%20Shared%20Mailbox%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-4413%22%20slang%3D%22en-US%22%3E%3CP%3EYou%20have%20two%20options%20here%3A%20use%20some%20EWS%20based%20script%20(Glen's%20blog%20is%20usually%20the%20best%20resource%20for%20anything%20EWS%20related%3A%20%3CA%20href%3D%22http%3A%2F%2Fgsexdev.blogspot.bg%2F2010%2F01%2Fwriting-simple-scripted-process-to.html%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttp%3A%2F%2Fgsexdev.blogspot.bg%2F2010%2F01%2Fwriting-simple-scripted-process-to.html%3C%2FA%3E)%20or%20use%20the%20Outlook%20COM%20object%20model%20if%20you%20have%20a%20single%20shared%20mailbox%20that%20you%20have%20configured%20in%20Outlook%20(example%20here%20%3CA%20href%3D%22https%3A%2F%2Fsocial.technet.microsoft.com%2FForums%2Fexchange%2Fen-US%2Fc4d4cca8-2bc0-48a9-97f8-61fc68c8079c%2Fpowershell-script-to-get-all-new-attachments-and-store-them-on-a-network-share%3Fforum%3Dexchange2010%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fsocial.technet.microsoft.com%2FForums%2Fexchange%2Fen-US%2Fc4d4cca8-2bc0-48a9-97f8-61fc68c8079c%2Fpowershell-script-to-get-all-new-attachments-and-store-them-on-a-network-share%3Fforum%3Dexchange2010%3C%2FA%3E)%3C%2FP%3E%3C%2FLINGO-BODY%3E
Brent Ellis
Valued 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?

4 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.
Related Conversations
Receiving emails is delayed
Kristen_100 in Microsoft Teams on
1 Replies
Outlook (365) Need Password - Issue
Muhammad Ali Khan in Office 365 on
21 Replies
Accessing a shared mailbox from a mobile device
Hexsysadmins in Office 365 on
14 Replies