Powershell Script to check HP Warranty information for PC’s and Laptops

Powershell Script to check HP Warranty information for PC’s and Laptops

Powershell HP Warranty

Registering for the HP Developer Warranty API

In order for this script to work you will need to register on the HP Developer portal and gain access to the HP Warranty API.  Once registered you will be given your own HP API Key and Secret.  For more information on how to do this, please see here:

https://developers.hp.com/hp-client-management/doc/warranty-api-keys-0


What does this Script do?

The first part of the script will query the local computer or laptop to obtain the Product Number and Serial Number of the PC. 

It will then Query the HP Warranty API using your API Key for the following information:

Product Model
Warranty Status
Warranty Start Date
Warranty End Date
Warranty Service Level

The Second part of the Script will then email this information to an email address of your choice.  You will have to specify your Email Settings within the Script.

Note – This will NOT work for HPE Servers as they have a different HPE API.

Things you will need to edit:

You will need to update the following parts of the Script with your relevant information:

$APIKey
$APISecret
$FromAddress
$ToAddress
$SMTPServer

The Script:

#API Information - Configuration Required 
$URI = "https://css.api.hp.com/oauth/v1/token" 
$APIKey = "Insert API Key" 
$APISecret = "Insert API Secret" 
 
#Get PC Name and Domain Name - Some Configuration Required 
$PCName = $Env:COMPUTERNAME 
$Domain = $env:USERDNSDOMAIN 
$FromAddress = "Insert Email Address" 
$ToAddress = "Insert Email Address" 
$Subject = "HP Warranty Information for $PCName.$Domain" 
$SMTPServer = "Insert Email Server" 
 
 
#Starting the Script - No further Configurtion Required 
#Creating API String 
$APIString = "apiKey=$APIKey&apiSecret=$APISecret&grantType=client_credentials&scope=warranty" 
 
 
Write-Host "Checking HP Warranty of local PC" -ForegroundColor Green 
 
$Product = Get-WmiObject win32_computersystem 
$Product = $Product.SystemSKUNumber 
Write-Host "HP Product Number is $Product" -ForegroundColor Yellow 
 
$Serial = Get-WmiObject Win32_BIOS 
$Serial = $Serial.SerialNumber 
Write-Host "HP Product Number is $Serial" -ForegroundColor Yellow 
 
#Check Against HP API 
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 
$authKey = Invoke-WebRequest -Method Post -uri $URI -Body $APIString 
$ContentOrigial = $authKey.Content 
$content = $ContentOrigial.Replace('<?xml version="1.0" encoding="UTF-8"?><Root><access_token>', "") 
$Content = $Content.Replace('</access_token><expires_in>3599</expires_in><scope>warranty</scope></Root>', "") 
 
 
$headers = @{} 
$headers.add("Authorization","Bearer $Content") 
$headers.add("accept","application/json") 
$headers.add("content-type","application/json") 
$body = "[{ `"sn`": `"$serial`", `"pn`": `"$Product`" }]" 
$results = Invoke-WebRequest -Method Post -Uri "https://css.api.hp.com/productWarranty/v1/queries" -Headers $headers -Body $body 
$results.Content | ConvertFrom-Json 
$emailbody = $results.Content | ConvertFrom-Json | Out-String 
 
Write-Host "Emailing Results" -ForegroundColor Yellow 
 
$email = @{ 
From = "$FromAddress" 
To = "$ToAddress" 
Subject = "$Subject" 
SMTPServer = "$SMTPServer" 
Body = $emailbody 
} 
 
send-mailmessage @email 
 
Write-Host "Script Complete - Email sent to $ToAddress" -ForegroundColor Green

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.