Powershell Invoke-RestMethod 到 Return JSON

Powershell Invoke-RestMethod To Return JSON

我每天都使用 PowerShell 运行 php 脚本。我似乎无法从 Invoke-RestMethod 取回 json return。如何实现?

这是我使用的代码:

$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 ---------------------------------------
}

PHP 脚本传回一个 json 字符串,例如:

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

您不需要 .resultsInvoke-RestMethod 自动将 JSON 转换为 [PSObject]

如果你想获得原始 JSON 然后使用 Invoke-WebRequest,但是因为你引用了 $resp.date$resp.num 看起来你想要它进行转换毕竟是为了你。