0

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

6
  • 1
    If I copy-paste your JSON, and do $o=(Get-Clipboard | ConvertFrom-Json) ; $o.AssetWarrantyResponse.AssetHeaderData.ServiceTag I get back XXXXXXX. Commented Feb 9, 2018 at 12:03
  • @JeroenMostert: that's because I replaced my service tag with X's ... Commented Feb 9, 2018 at 12:13
  • 1
    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. Commented Feb 9, 2018 at 12:16
  • if I run the correct URL in a browser, I get valid JSON back with the values Commented Feb 9, 2018 at 12:24
  • @JeroenMostert: how do I get the Get-Clipboard cmdLet to work in PS? Commented Feb 9, 2018 at 12:29

1 Answer 1

1
$req = Invoke-RestMethod $request
$warranties = $req.AssetWarrantyResponse.AssetEntitlementData[0]
$dellasset  = $req.AssetWarrantyResponse.productheaderdata
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.