Share via

Powershell and Invoke-restmethod harvesting attachments from Office 365 mail.

Anonymous
2018-02-24T10:05:13+00:00

I am having issues using invoke-restmethod to harvest attachments from emails.  All I want to do is place the attachment in a folder, and delete the email for multiple email accounts.  It works, however it errors on any email with an attachment larger than 10MB, and will only grab 6-10 emails at a time.  Some of my emails have attachments up to 100MB.  

Invoke-RestMethod : The remote server returned an error: (400) Bad Request.

At D:\TBIMailPoll\TBIMailPoll.ps1:104 char:24

  • ...      $attachments = Invoke-RestMethod $query -Credential $Credentials

+                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  • CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
  • FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Invoke-RestMethod : The remote server returned an error: (400) Bad Request.

At D:\TBIMailPoll\TBIMailPoll.ps1:104 char:24

  • ...      $attachments = Invoke-RestMethod $query -Credential $Credentials

+                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  • CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
  • FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

And this is the piece of code I am using.  The $query simply asks for all mail with attachments.

$query=$url+"/"+$message.Id +"/attachments"

$attachments = Invoke-RestMethod $query -Credential $Credentials

foreach ($attachment in $attachments.value ){  }

Is there a better way to do this?  Am I missing a parm to accommodate larger attachments? I have scoured the web, and seen no palatable answers.

Microsoft 365 and Office | Subscription, account, billing | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

2 answers

Sort by: Most helpful
  1. Anonymous
    2018-02-27T15:57:10+00:00

    Hi DiabloV,

    Please let me know if I can be of further assistance.

    Regards,

    Hans

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2018-02-24T14:05:41+00:00

    Hi DiabloV,

    Would you please provide the entire code for me to check the details. Meanwhile, does the Office 365 account is using Exchange online? You may provied the address of the account as well.

    Please provide the code & account address via private message for privacy.

    Regards,

    Hans

    Was this answer helpful?

    0 comments No comments