0
votes

I am having trouble using Invoke-RestMethod in PS and retrieving the results...

here is my code:

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

The JSON returned by the DELL API call looks like this:

{
"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
}

I need to get several values from that JSON response, I tried using ConvertFrom-Json with |Select ServiceTag, SystemDescription, EndDate by following the advice in MS TechNet Scripting Guy: Playing with JSON and PowerShell

But couldn't get values for ServiceTag, SystemDescription, EndDate (they were blank) - eventually we need to run this in a script for nearly 1500 computers in blocks of 80 at a time and update a database

So where am I going wrong here?

I did try something like Invoke-RestMethod $request | Select $request.AssetHeaderData.ServiceTag but had no luck

1
If I copy-paste your JSON, and do $o=(Get-Clipboard | ConvertFrom-Json) ; $o.AssetWarrantyResponse.AssetHeaderData.ServiceTag I get back XXXXXXX. - Jeroen Mostert
@JeroenMostert: that's because I replaced my service tag with X's ... - Our Man in Bananas
Yes, but it demonstrates the basic approach works: I can get the service tag value with my code, so the question is why yours can't. If the tag is actually blank in the JSON itself, your issue is with Dell, not the code extracting the value. - Jeroen Mostert
if I run the correct URL in a browser, I get valid JSON back with the values - Our Man in Bananas
@JeroenMostert: how do I get the Get-Clipboard cmdLet to work in PS? - Our Man in Bananas

1 Answers

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