Forum Discussion
Brent Ellis
Aug 04, 2016Silver Contributor
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?
Brent Ellis
Aug 04, 2016Silver 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
}
VasilMichev
Aug 04, 2016MVP
Oh right, we also have REST APIs now :) Gotta start learning those...
- Adam OchsAug 10, 2016Iron ContributorWhat 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.