读取Invoke-RestMethod返回的powershell对象

时间:2018-02-09 11:58:40

标签: json powershell

我在PS中使用Invoke-RestMethod并检索结果时遇到问题......

这是我的代码:

$request='https://sandbox.api.dell.com/support/assetinfo/v4/getassetwarranty/SERVICETAG?apikey=APIKEY'
Invoke-RestMethod $request |
Select $request.ServiceTag 

DELL API调用返回的JSON如下所示:

{
"AssetWarrantyResponse": [
{

  "AssetHeaderData": {
    "BUID": "202",
    "ServiceTag": "XXXXXXX",
    "ShipDate": "2017-12-04T18:00:00",
    "CountryLookupCode": "UK",
    "LocalChannel": "ENTP",
    "CustomerNumber": "NNNNNN",
    "ItemClassCode": "OB002",
    "IsDuplicate": false,
    "MachineDescription": "Latitude 7480",
    "OrderNumber": "123456789",
    "ParentServiceTag": null
  },
  "ProductHeaderData": {
    "SystemDescription": "Latitude 7480",
    "ProductId": "latitude-14-7480-laptop",
    "ProductFamily": "Laptops",
    "LOB": "Latitude",
    "LOBFriendlyName": "Latitude"
  },
  "AssetEntitlementData": [
    {
      "StartDate": "2017-12-04T18:00:00",
      "EndDate": "2020-12-05T17:59:59",
      "ServiceLevelDescription": "Onsite Service After Remote Diagnosis (Consumer Customer)/ Next Business Day Onsite After Remote Diagnosis (Commercial Customer)",
      "ServiceLevelCode": "ND",
      "ServiceLevelGroup": 5,
      "EntitlementType": "INITIAL",
      "ServiceProvider": null,
      "ItemNumber": "123-12345"
    },
    {
      "StartDate": "2017-12-04T06:00:00",
      "EndDate": "2025-12-05T05:59:59",
      "ServiceLevelDescription": "Dell Digitial Delivery",
      "ServiceLevelCode": "D",
      "ServiceLevelGroup": 11,
      "EntitlementType": "INITIAL",
      "ServiceProvider": null,
      "ItemNumber": "525-10302"
    }
   ]
  }
],
"InvalidFormatAssets": { "BadAssets": [ ] },
"InvalidBILAssets": { "BadAssets": [ ] },
"ExcessTags": { "BadAssets": [ ] },
"AdditionalInformation": null
}

我需要从该JSON响应中获取多个值,我尝试使用ConvertFrom-Json|Select ServiceTag, SystemDescription, EndDate,并遵循MS TechNet Scripting Guy: Playing with JSON and PowerShell中的建议

但是无法获得ServiceTag,SystemDescription,EndDate的值(它们是空白的) - 最终我们需要在一个脚本中以近80个块的方式运行它,并且更新数据库

那我在哪里错了?

我确实尝试了类似Invoke-RestMethod $request | Select $request.AssetHeaderData.ServiceTag但没有运气

的东西

1 个答案:

答案 0 :(得分:1)

$req = Invoke-RestMethod $request
$warranties = $req.AssetWarrantyResponse.AssetEntitlementData[0]
$dellasset  = $req.AssetWarrantyResponse.productheaderdata