powershell https://outlook.office365.com/api/v1.0/me/messages

751 views Asked by At

I had the below Powershell working perfectly until last November the 1st.

$url = "https://outlook.office365.com/api/v1.0/me/messages" 
$date = Get-Date -Format "yyyy-MM-d"

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

I get the below error: Invoke-RestMethod : The remote server returned an error: (400) Bad Request. At G:\Powell\AutoetForO.ps1:23 char:13 + $messages = Invoke-RestMethod $messageQuery -Credential $cred + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Per my investigation; I know its not credential issues; I think they changed their API.

1

There are 1 answers

0
Andrew Cooper On BEST ANSWER

Running the same script with Fiddler capturing traffic I found the body of the error response was:

92
{"error":{"code":"RequestBroker-ParseUri","message":"Syntax error at position 54 in 'HasAttachments eq true and DateTimeReceived ge 2017-01-5'."}}
0

The date looked like the problem. Changing your second line to the following fixed the problem:

$date = Get-Date -Format "yyyy-MM-dd"