【问题标题】:Powershell Invoke-RestMethod To Return JSONPowershell Invoke-RestMethod 返回 JSON
【发布时间】:2015-06-20 12:43:18
【问题描述】:

我每天都在使用 PowerShell 运行一个 php 脚本。我似乎无法从Invoke-RestMethod 返回json。怎么实现?

这是我正在使用的代码:

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

【问题讨论】:

    标签: json powershell powershell-2.0 psobject


    【解决方案1】:

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

    如果您想获取原始 JSON,请使用 Invoke-WebRequest,但由于您引用了 $resp.date$resp.num,看来您毕竟希望它为您进行转换。

    【讨论】:

      猜你喜欢
      • 2015-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多