0

I have created a script in PowerShell 5.1 that retrieves mail messages not older that one day with 'report' as a subject and save their attachments into local drive. The problem is that in the production environment I have only Powershell 2.0. I am using Invoke-RestMethod in my code like this:

$url = "https://outlook.office365.com/api/v1.0/me/messages" 
$date = (get-date).AddDays(-1).ToString("yyyy-MM-dd")
$subject = "'report'"
$messageQuery = "" + $url + "?`$select=Id&`$filter=HasAttachments eq true and DateTimeReceived ge " + $date + " and Subject eq " + $subject
$messages = Invoke-RestMethod $messageQuery -Credential $cred 

foreach ($message in $messages.value) 
{ 
    $query = $url + "/" + $message.Id + "/attachments" 
    $attachments = Invoke-RestMethod $query -Credential $cred 

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

        # SAVE ATTACHMENT CODE HERE
    }
}

Is there a simple way to convert the code in order to be suitable for PowerShell 2.0?

szach
  • 1
  • 1
  • 1
    [Try This?](https://social.technet.microsoft.com/Forums/scriptcenter/en-US/64c74e89-610e-4229-a56b-12f973232a0a/replace-invokerestmethod-in-powershell-20?forum=ITCG) – colsw Jul 25 '17 at 12:24

1 Answers1

0
Invoke-WebRequest

This command is basically the same as Invoke-RestMethod except in how it handles the data after it receives it. You are going to have to make some small modifications on how you parse your data.

I'll wager you are receiving JSON data so you would just run your Invoke-WebRequest command and pipe it to ConvertFrom-JSON and assign the results to a var. This will let you then do something like $x.messages | % { $_ }

You will need to implement this converter in 2.0. You can copy and paste from: PowerShell 2.0 ConvertFrom-Json and ConvertTo-Json implementation

XML is supported in PS 2.0 natively.

henrycarteruk
  • 12,708
  • 2
  • 36
  • 40
Ty Savercool
  • 1,132
  • 5
  • 10
  • Thank you for the answer but I don't know how to use it properly, could you please give me some example? – szach Jul 26 '17 at 06:50