PowerShell to gather HP Warranty information

Using PowerShell to get HP warranty information

Have you ever need to find the warranty information for your environment’s workstations, and didn’t want to spend hours to get the answers.  That is where PowerShell and HP warranty API will come in handy.

Sign up for an HP Developer’s account

Before you can begin you will need to sign up for an account on HP’s website, https://developers.hp.com/.  Once accepted, you will also need to apply for the Product Warranty API, https://developers.hp.com/css-enroll.  You will have to wait for the approval process for this which when I signed up I got approved within 24 hours.

After the API approval process has been completed.  Register an application within the portal.  The application registration will provide the required API keys and the API Endpoint to be able to perform the lookup.

Path for Using the HP API

The next step is getting the API credentials from the app that was just registered to use to get the OAuth token to be able to call the APIs.

HP API Credentials and Endpoint

Obtaining the OAuth Token

With the API credentials I can now make the first call to HP to get the OAuth token using the following script.

$authKey = Invoke-WebRequest -Method POST -uri "https://css.api.hp.com/oauth/v1/token" -Body "apiKey=xxxx&apiSecret=yyyy&grantType=client_credentials&scope=warranty"
$Content = ""
$Content = $authKey.Content
[string]$AccessToken = $Content.root.access_token | Out-String

*Replace the xxxx with the API KEY and yyyy with the API Secret

Using the HP Warranty API

This script will return the required token to be able to make the API call to get the warranty information. Now just need to form the HEADERS for the web request which is where the token from the last script will be used.  The API can be used either with just a serial number or with both serial number and part number.  The below script is using just the serial number.

$headers = @{}
$headers.add("Authorization","Bearer $AccessToken")
$headers.add("accept","application/json")
$headers.add("content-type","application/json")
$body = "[{ `"sn`": `"SerialNumberHere`" }]"
$results = Invoke-WebRequest -Method Post -Uri "https://css.api.hp.com/productWarranty/v1/queries" -Headers $headers -Body $body
$results.Content | ConvertFrom-Json

If you want to use both serial number and part number you will have to change the line

 $body = "[{ `"sn`": `"SerialNumberHere`" }]" 

to look like this format:

$body = "[ { `"sn`": `"SerialNumberHere`", `"pn`": `"PartNumberHere`" }]"

The output from the script will look like the following.

HP API Results

*Note: this API call is only for HP desktop, workstations and laptop.  As far as I have found there isn’t an API call for HPE (HP Enterprise) Servers.

-Stuart

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.