Forum Discussion
dgillespie-adf
Jun 09, 2020Brass Contributor
Powershell script to export all attachments from a user mailbox and save them locally
So it seems there are a bazillion ways to do this but I settled on the simplest one and I am getting an Invoke-RestMethod error. Office 365 exchange online and I am testing with my user account and I am a global admin. Any help is greatly appreciated - thanks.
The method I chose is listed here (The Brent Ellis reply): https://techcommunity.microsoft.com/t5/exchange/need-an-example-powershell-script-to-get-attachments-from-an/m-p/4400
Here is my script:
Connect-EXOPSSession -UserPrincipalName xxx@xxx..com
$Mailbox = "myuser@xxx.com"
$url = "https://outlook.office365.com/api/v2.0/users/$Mailbox/messages"
$date = "2020-06-01"
## 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:\Attachments\" + $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
}
The error I get is:
Invoke-RestMethod : The remote server returned an error: (401) unauthorized
At C:\new-pw-dl-attach.ps1:9 char:13
+ $messages = Invoke-RestMethod $messageQuery -Credential $cred
+ categoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft,Powershell.Commands.InvokeRestMethodCommand
- Jattar74Copper Contributor
Try using this to pass the user credentials,
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
- BemmelenPatrickIron ContributorHello Derek,
I would advice you to take a look at New-ComplianceSearch -ContentMatchQuery and include the KQL query "hasattachments:true":
https://docs.microsoft.com/en-us/powershell/module/exchange/new-compliancesearch?view=exchange-ps
After that you can export the results to a PST file.
Take a look at example 4:
https://docs.microsoft.com/en-us/powershell/module/exchange/new-compliancesearchaction?view=exchange-ps#examples