1
votes

I am using PowerShell to run a php script on a daily basis. I can't seem to get the json return back from Invoke-RestMethod. How can that be accomplished?

Here is the code I am using:

$limit = 200;
for ($i=1; $i -le $limit; $i++)
{                          
    Write-Host $i started of $limit
    $Site = "http://example.com/updates"
    $Info = Measure-Command{
        $data = @{call_type = 'powershell'}
        $resp = (Invoke-RestMethod -Method Post -URI $Site -Body $data -TimeoutSec 500).results       
    }
    Write-Host resp - On $resp.date, $resp.num were downloaded 
    Write-Host $i took $Info.TotalSeconds s 
    Write-Host ---------------------------------------
}

The PHP script is passing back a json string like:

{date: '2014-09-30', num: 127569}
1

1 Answers

4
votes

You don't need .results. Invoke-RestMethod converts JSON into a [PSObject] automatically.

If you want to get the raw JSON then use Invoke-WebRequest, but since you reference $resp.date and $resp.num it seems like you wanted it to do the conversion for you after all.